我正在尝试使用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]
匹配,但无法完成。如果您真的想这样做,而不必担心
j
和k
到底是什么,只需使用*
运算符,然后: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/