python(GUI)中的KeyError

2024-05-29 07:42:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了这个GUI,它包含一个列表框、一个输入框和一个OK按钮。你知道吗

这是保存到硬盘驱动器中的词典(pickled):

dict1 = {1: ['Banquet Burger', 80, 19], 
         2: ['Veg Burger', 40, 14], 
         3: ['Chicken Burger', 68, 15], 
         4: ['Cheese Burger', 50, 13]}

在entry小部件中,当用户输入产品代码(key)时,应该将项目插入到listbox中。你知道吗

例如,如果用户键入1,则应将'Banquet Burger'添加到列表框中。如果他键入3'Chicken Burger'应该被添加到列表中(但是'Banquet Burger'应该仍然存在,它不应该消失)。你知道吗

这段代码给了我一个KeyError。我确信我已经导入了所有必要的模块。你知道吗

def OK_button():
    with open('Stock Burgers.txt', 'r') as file:
        dict1=pickle.load(file)
        code=product_code.get()
        code1=int(code)
        product_info=dict1[code]
        item=product_info[0]
        items.insert(1, item)

product_code=Entry(mcD, bd=7)
product_code.grid(row=3, columnspan=1)
ok_button=Button(mcD, text='   OK   ', command=OK_button)
ok_button.grid(row=4, columnspan=3)
items=Listbox(mcD, height=10, width=22)
items.grid(row=2, column=0)
mcD.mainloop()

Tags: 代码用户codeitemsbuttonokproductgrid

热门问题