如何让列表框打印包含项目名称和价格的字典- Python

How to get a listbox to print a dictionaries containig item names and prices- Python

所以我有一本字典存储食品及其价格,如下所示:

Menu = {"Mozerella":"£3.50", "Haiwann":"£3.99", "New York Style":"£4.10", "Chicken Gougons":"£3.80"}

然而,当我运行这个片段

choices = Menu
tkvar.set("Mozerella")
popupMenu = OptionMenu(Menu_Screen, tkvar, *choices)

它在列表框中仅显示为项目名称,没有附加价格。 如何将商品及其附加价格添加到列表框中。

要合并商品和价格,您可以使用列表理解合并字典键和值。这将创建一个新列表以供在下拉列表中使用。

试试这个代码:

import tkinter as tkr
root = tkr.Tk()

# textbox for testing
selText=tkr.StringVar()
e1=tkr.Entry(root, textvariable=selText, width=42)
e1.grid(row=0, column=1)

def OptionMenu_Select(e): # user selection changed
   selText.set(tkvar.get())  # update textbox

Menu = {"Mozerella":".50", "Haiwann":".99", "New York Style":".10", "Chicken Gougons":".80"}
choices = [m + "  " + Menu[m] for m in Menu]  # combine item and price

tkvar=tkr.StringVar()
tkvar.set(choices[0])  # set dropdown
selText.set(choices[0])  # set text box  
popupMenu = tkr.OptionMenu(root, tkvar, *choices, command = OptionMenu_Select)
popupMenu.grid(row=1, column=1)

root.mainloop()