简体   繁体   中英

asciimatics too many values to unpack (expected 2) error

I am trying to make a simple shop application with asciimatics module.It is supposed to be a app where you can browse products, like in a shop and then you can add or delete the item from your cart. I was using their contact list sample app as my guide, but when I wrote my code I got stuck at this error

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)

Here is my code - ShopDB class is just holding database object and Shop class is working with asciimatics module itself.

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

Seems like your self._options have more than two values after enumerating. That's why these values can't be assigned to [_, value] in your code. Example of this:

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

This code gives an error, because Python tries to match [1,2,3] to [j, k] , which can't be done. If your really want to do this and you don't have to worry about what exactly j and k are, just go with * operator, then:

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM