目前我使用tkinter制作了一个下拉菜单的基本模板,其中包含三个参数1,2,3。
from tkinter import *
master = Tk()
variable = StringVar()
variable.set("one") # default value
w = OptionMenu(master, variable, "one", "two", "three")
w.pack()
mainloop()
为了更清晰,请参见上图。
现在我有30个键值对的字典,如何将它们填充为OptionMenu中的选项。 词典:
"Belgium (Dutch)": "nl_be",
"Hungary": "hu_hu",
"Chile": "es_cl",
"Belgium (French)": "fr_be",
"Mexico": "es_mx",
"Hong Kong": "hk",
"Turkey": "tr_tr",
"Ireland": "en_ie",
"Ghana": "en_gh",
"Argentina": "es_ar",
"Slovakia": "sk_sk",
我只想将词典的键作为optionmenu中的选项。 而不是在OptionMenu()中传递字典的每个键是他们的任何智能技术。
答案 0 :(得分:3)
您可以使用*
将列表解压缩为多个参数
OptionMenu(master, variable, *data.keys())
完整
data = {
"Belgium (Dutch)": "nl_be",
"Hungary": "hu_hu",
"Chile": "es_cl",
"Belgium (French)": "fr_be",
"Mexico": "es_mx",
"Hong Kong": "hk",
"Turkey": "tr_tr",
"Ireland": "en_ie",
"Ghana": "en_gh",
"Argentina": "es_ar",
"Slovakia": "sk_sk",
}
from tkinter import *
master = Tk()
variable = StringVar()
variable.set("one") # default value
w = OptionMenu(master, variable, *data.keys())
w.pack()
mainloop()