gpt4 book ai didi

python - asciimatics 太多值无法解压(预期 2)错误

转载 作者:行者123 更新时间:2023-12-01 02:19:49 30 4
gpt4 key购买 nike

我正在尝试使用 asciimatics 模块制作一个简单的商店应用程序。它应该是一个您可以浏览产品的应用程序,就像在商店中一样,然后您可以从购物车添加或删除商品。我使用他们的联系人列表示例应用程序作为我的指南,但是当我编写代码时,我陷入了这个错误

kumecky@osmijanko:~/notebooks$ python3 form.py
Traceback (most recent call last):
File "form.py", line 103, in <module>
Screen.wrapper(demo, catch_interrupt=True, arguments=[last_scene])
File "/usr/local/lib/python3.5/dist-packages/asciimatics/screen.py", line 1167, in wrapper
func(screen, *arguments)
File "form.py", line 97, in demo
screen.play(scenes, stop_on_resize=True)
File "/usr/local/lib/python3.5/dist-packages/asciimatics/screen.py", line 1343, in play
scenes, unhandled_input=unhandled_input, start_scene=start_scene)
File "/usr/local/lib/python3.5/dist-packages/asciimatics/screen.py", line 1406, in set_scenes
old_scene=start_scene, screen=self)
File "/usr/local/lib/python3.5/dist-packages/asciimatics/scene.py", line 44, in reset
effect.reset()
File "/usr/local/lib/python3.5/dist-packages/asciimatics/widgets.py", line 552, in reset
self.data = deepcopy(self._initial_data)
File "/usr/local/lib/python3.5/dist-packages/asciimatics/widgets.py", line 475, in data
layout.update_widgets()
File "/usr/local/lib/python3.5/dist-packages/asciimatics/widgets.py", line 1162, in update_widgets
widget.value = widget.value
File "/usr/local/lib/python3.5/dist-packages/asciimatics/widgets.py", line 2224, in value
for i, [_, value] in enumerate(self._options):
ValueError: too many values to unpack (expected 2)

这是我的代码 - ShopDB 类只是保存数据库对象,而 Shop 类正在与 asciimatics 模块本身一起使用。

from asciimatics.widgets import Frame, ListBox, Layout, Divider, Text, \
Button, TextBox, Widget
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError, NextScene, StopApplication
import sys
import sqlite3

class ShopDB(object):
def __init__(self):
# vytvorí databazu v RAM pamati
self.db = sqlite3.connect(':memory:')
self.db.row_factory = sqlite3.Row

self.db.cursor().execute('''
CREATE TABLE products(
id INTEGER PRIMARY KEY,
product TEXT,
price REAL,
notes TEXT,
in_cart INTEGER)
''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(1, "Mlieko", 0.59, "Veľmi lahodné plnotučné kravské mlieko.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(2, "Maslo", 1.59, "Kvalitné maslo priamo z vidieka.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(3, "Chlieb", 1.19, "Čerstvý chlieb.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(4, "Med", 3.49, "Veľký pohár medu.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(5, "Jablko", 0.49, "Zdravé jabĺčko.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(6, "Zemiaky 5kg", "0.60", "Vrecko zemiakov.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(7, "Šiška", "0.99", "Čerstvá a mäkká šiška.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(8, "Soľ", "0.29", "Soľ nad zlato.",0 )''')
self.db.cursor().execute('''INSERT INTO products(id, product, price, notes, in_cart) VALUES(9, "Cukor", "1.19", "Kryštálový cukor.",0 )''')
self.db.commit()

def addToCart(self, product_id):
self.db.cursor().execute('''UPDATE products SET in_cart=1 WHERE id='''+product_id)
self.db.commit()

def removeFromCart(self, product_id):
self.db.cursor().execute('''UPDATE products SET in_cart=0 WHERE id='''+product_id)
self.db.commit()

def showAllProducts(self):
return self.db.cursor().execute('''SELECT * FROM products''').fetchall()

def showCart(self):
return self.db.cursor().execute('''SELECT * FROM products WHERE in_cart=1''').fetchall()

class Shop(Frame):
def __init__(self, screen, db):
super(Shop, self).__init__(screen,
screen.height * 2 // 3,
screen.width * 2 // 3,
on_load=self.loadContent,
hover_focus=True,
title="Obchod ~~ ASCIIMATICO ~~")
self.model = db

self.productsList = ListBox(Widget.FILL_FRAME, db.showAllProducts(), name="shop")
self.addButton = Button("Pridaj do košíka", self.addToCart)
#self.deleteButton = Button("Odstráň z košíka", self.deleteFromCart)
self.showCartButton = Button("Ukáž košík", self.showCart)

listLayout = Layout([100], fill_frame=True)
self.add_layout(listLayout)
listLayout.add_widget(self.productsList)
listLayout.add_widget(Divider())

buttonLayout = Layout([1,1,1,1])
self.add_layout(buttonLayout)
buttonLayout.add_widget(self.addButton, 0)
buttonLayout.add_widget(self.showCartButton, 3)
self.fix()

def loadContent(self):
self.productsList.options = self.model.showAllProducts()

def addToCart(self):
self.save()
self._model.current_id = self.data["contacts"]
raise NextScene("Product Detail")

def showCart(self):
self.save()
raise NextScene("Cart")


#class Cart(Frame):

#class ProductDetails(Frame):



def demo(screen, scene):
scenes = [
Scene([Shop(screen, database)], -1, name="Shop")
]
screen.play(scenes, stop_on_resize=True, start_scene=scene)

database = ShopDB()
last_scene = None
while True:
try:
Screen.wrapper(demo, catch_interrupt=True, arguments=[last_scene])
sys.exit(0)
except ResizeScreenError as e:
last_scene = e.scene

最佳答案

似乎您的 self._options 在枚举后有两个以上的值。这就是为什么这些值不能分配给代码中的 [_, value] 的原因。示例:

sample = [[1,2,3], [4,5,6]]
enums = list(enumerate(sample))
for i, [j, k] in enums:
print(i, j, k)

此代码会出错,因为 Python 尝试将 [1,2,3][j, k] 匹配,但无法完成。如果您确实想这样做,并且不必担心 jk 到底是什么,只需使用 * 运算符即可,那么:

sample = [[1,2,3], [4,5,6]]
enums = list(enumerate(sample))
for i, [j, *k] in enums:
print(i, j, k)

输出:

0 1 [2, 3]
1 4 [5, 6]

关于python - asciimatics 太多值无法解压(预期 2)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48061206/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com