我的字典功能不正常

时间:2018-05-04 07:03:14

标签: python-3.x dictionary

我正在研究一个tkinter项目。它只是一个简单的餐厅收银员(无论你怎么称呼)项目。它还没有完成,但我遇到了问题。

这是我的代码:

CREATE PROCEDURE dbo.YourProcedure
    @dealercode VARCHAR(20)
AS
BEGIN

    BEGIN TRY

        -- Your operations here
        SELECT 1 / 0

    END TRY

    BEGIN CATCH

        DECLARE @v_ErrorMessage VARCHAR(MAX) = CONVERT(VARCHAR(MAX), ERROR_MESSAGE())

        -- Rollback if you have to

        RAISERROR (@v_ErrorMessage, 16, 1)

    END CATCH

END

我将引导您完成代码:

  1. 宣布商店以字典销售的商品
  2. 创建一个类,使对象能够创建商店中销售的每种类型的项目
  3. 在下面的代码中声明获取条目和选项菜单的变量。
  4. 制作条目
  5. 网格化
  6. 创建一个获取条目和选项菜单的函数,并将其设置为上面的变量。
  7. 我认为这里的item_selected是不必要的。

    所以,这里的问题是当我跑步时,它给我这样的东西:

    from tkinter import *
    from tkinter.ttk import Style
    
    # The different type of items that the store is selling
    drinks = {}
    burgers = {}
    french_fries = {}
    ice_creams = {}
    
    class Object:
    
        def __init__(self, name, price, type):
            self.name = name
            self.price = price
            self.type = type
            exec('{}["{}"] = [{}, {}]'.format(self.type, name, price, self.type))
    
        def return_price(self):
            return self.price
    
        def get_price(self, amount):
            return self.price * amount
    
    # Creating the object
    Object('Coco Cola', 1.50, 'drinks')
    Object('Sprite', 1.50, 'drinks')
    Object('Apple Juice', 2.50, 'drinks')
    Object('Orange juice', 2.50, 'drinks')
    Object('Grape Juice', 2.50, 'drinks')
    Object('Cheese and Ham', 3.75, 'burgers')
    Object('Cheese', 4.00, 'burgers')
    Object('Cheese and Fish', 3.50, 'burgers')
    Object('All Meat', 5.50, 'burgers')
    Object('Waffle Fries', 2.50, 'french_fries')
    Object('Chili Cheese Fries', 2.75, 'french_fries')
    Object('Polenta Fries', 3.50, 'french_fries')
    Object('Potato Wedges', 3.50, 'french_fries')
    Object('Strawberry', 3.50, 'ice_creams')
    Object('Blueberry', 3.75, 'ice_creams')
    Object('Black Berry', 3.00, 'ice_creams')
    Object('Vanilla', 3.00, 'ice_creams')
    Object('Chocolate', 3.50, 'ice_creams')
    
    
    
    # This code is what gives me the result i specified at the bottom of my question
    print(drinks)
    
    # All the tkinter stuff
    root = Tk()
    root.resizable(width=False, height=False)
    root.style = Style()
    root.style.theme_use('xpnative')
    
    
    drink, burger, french_fry, ice_cream = StringVar(), StringVar(), StringVar(), StringVar()
    drink_amount, burger_amount, french_fry_amount, ice_cream_amount = IntVar(), IntVar(), IntVar(), IntVar()
    subtotal, tax, total, change = IntVar(), IntVar(), IntVar(), IntVar()
    drink_amount.set(0)
    burger_amount.set(0)
    french_fry_amount.set(0)
    ice_cream_amount.set(0)
    
    Label(root, text='Restaraunt Cashier').grid(row=0, column=1, columnspan=5)
    Label(root, text='Drinks:').grid(row=1, sticky=W)
    Label(root, text='Burgers:').grid(row=2, sticky=W)
    Label(root, text='French Fries:').grid(row=3, sticky=W)
    Label(root, text='Ice cream:').grid(row=4, sticky=W)
    Label(root, text='Amount:').grid(row=1, column=2, sticky=E)
    Label(root, text='Amount:').grid(row=2, column=2, sticky=E)
    Label(root, text='Amount:').grid(row=3, column=2, sticky=E)
    Label(root, text='Amount:').grid(row=4, column=2, sticky=E)
    Label(root, text='Subtotal:').grid(row=1, column=4, sticky=W)
    Label(root, text='Tax:').grid(row=2, column=4, sticky=W)
    Label(root, text='Total:').grid(row=3, column=4, sticky=W)
    Label(root, text='Change:').grid(row=4, column=4, sticky=W)
    
    # Entry types
    drink_entry = OptionMenu(root, drink, *drinks)
    burger_entry = OptionMenu(root, burger, *drinks)
    french_fries_entry = OptionMenu(root, french_fry, *drinks)
    ice_cream_entry = OptionMenu(root, ice_cream, *drinks)
    
    # Entry amount
    drink_entry_amount = Entry(root, width=50)
    burger_entry_amount = Entry(root, width=50)
    french_fry_entry_amount = Entry(root, width=50)
    ice_cream_entry_amount = Entry(root, width=50)
    
    # Subtotal, tax, total, change, entries
    subtotal_entry = Entry(root, width=50)
    tax_entry = Entry(root, width=50)
    total_entry = Entry(root, width=50)
    change_entry = Entry(root, width=50)
    
    # Gridding
    drink_entry.grid(row=1, column=1, ipady=5)
    burger_entry.grid(row=2, column=1, ipady=5)
    french_fries_entry.grid(row=3, column=1, ipady=5)
    ice_cream_entry.grid(row=4, column=1, ipady=5)
    drink_entry_amount.grid(row=1, column=3, ipady=5)
    burger_entry_amount.grid(row=2, column=3, ipady=5)
    french_fry_entry_amount.grid(row=3, column=3, ipady=5)
    ice_cream_entry_amount.grid(row=4, column=3, ipady=5)
    subtotal_entry.grid(row=1, column=5, ipady=5)
    tax_entry.grid(row=2, column=5, ipady=5)
    total_entry.grid(row=3, column=5, ipady=5)
    change_entry.grid(row=4, column=5, ipady=5)
    
    entry_list = [
        'drink_entry', 'burger_entry', 'french_fries_entry', 'ice_cream_entry', 'drink_entry_amount', 'burger_entry_amount',
        'french_fries_entry_amount', 'ice_cream_entry_amount'
    ]
    
    item_selected = []
    
    def submit_clicked(event=None):
        global item_selected
        drink_amount.set(drink_entry_amount.get())
        burger_amount.set(burger_entry_amount.get())
        french_fry_amount.set(french_fry_entry_amount.get())
        ice_cream_amount.set(ice_cream_entry_amount.get())
        item_selected.append(drink.get())
        item_selected.append(burger.get())
        item_selected.append(french_fry.get())
        item_selected.append(ice_cream.get())
        item_selected = list(filter(None, item_selected))
    
    
    
    submit_button = Button(root, text='Submit', command=submit_clicked)
    submit_button.grid(row=6, column=1, columnspan=5)
    root.grid_rowconfigure(5, minsize=20)
    
    
    root.mainloop()
    

    我对代码的期望:

    {'Coco Cola': [1.5, {...}], 'Sprite': [1.5, {...}], 'Apple Juice': [2.5, {...}], 'Orange juice': [2.5, {...}], 'Grape Juice': [2.5, {...}]}
    

    我相信我的问题不会干扰tkinter代码,我可能错了,但我只是希望别人找出我的错误,因为我现在不能。

1 个答案:

答案 0 :(得分:1)

正如我认为@ Aran-Fey在评论中被诊断出来的那样,您的问题与exec Object.__init__的电话有关,而exec('{}["{}"] = [{}, {}]'.format(self.type, name, price, self.type)) 电话没有按照您的预期进行。什么时候做:

self.type

"drinks"等于您的某个词典的名称(如drinks[...] = [..., drinks] ),您正在运行此Python语句:

"drinks"

也就是说,您将字典的引用分配为字典中值的一部分。我怀疑您希望该值包含字符串drinks,而不是对名为{!r}的字典的引用。为此,您需要格式字符串中的exec

但更好的是,完全取消"drinks"电话!变量名称不应该是数据。如果您有一个类似字符串drinks = {} burgers = {} french_fries = {} ice_creams = {} 的数据项,并且您希望它能让您查找字典,请将其用作另一级字典中的键。

所以而不是四个顶级变量:

data

只做一个(我已经命名为data = {"drinks": {}, "burgers": {}, "french_fries": {}, "ice_creams": {}} ,你可能会想出一个更好的名字):

exec

然后,您可以根据需要将字典编入索引,而不是需要data[self.type][name] = [price, self.type]

Object

您的代码中还存在其他一些问题。你为副作用专门创建TKinter个实例是非常奇怪的(你在每个实例创建后立即丢弃它们)。如果构造函数将对象插入到数据结构中(例如,Object.__init__类型经常这样做),这可能是有意义的。如果您只关心副作用,只需将代码从Entry移到顶级函数中,然后取消该类!

您的几个burger_entry = OptionMenu(root, burger, *drinks)对象似乎引用了错误的字典:*burgers应该有*data['burgers']作为其最后一个参数(或MATCH (s:Product {id:'5001207'})-[r]->(o:ProdAttrs) WHERE any(key in [prop in keys(o) | toLower(prop)] WHERE key contains 'network' OR key contains 'phone' OR key contains 'smart') RETURN o 如果更改数据结构正如我上面所建议的那样。)