Python 是否可以在tkinter/ttk中使用HTML optgroup样式的选项分组创建下拉菜单?
我想使用数组字典中的tkinter创建一个下拉菜单,其中的键表示菜单选项的高级组,值数组中的字符串表示用户可用的实际选项。换句话说,单击下拉列表时,下拉列表应该类似于使用optgroup标记在HTML中创建的下拉列表,其中既有作为组标题的不可选择标签,也有作为实际选项的可选择标签 我的字典看起来像这样:Python 是否可以在tkinter/ttk中使用HTML optgroup样式的选项分组创建下拉菜单?,python,tkinter,optgroup,Python,Tkinter,Optgroup,我想使用数组字典中的tkinter创建一个下拉菜单,其中的键表示菜单选项的高级组,值数组中的字符串表示用户可用的实际选项。换句话说,单击下拉列表时,下拉列表应该类似于使用optgroup标记在HTML中创建的下拉列表,其中既有作为组标题的不可选择标签,也有作为实际选项的可选择标签 我的字典看起来像这样: ingredients = { "Herbs": ["basil", "oregano", "thyme"], "Meats":
ingredients = {
"Herbs":
["basil",
"oregano",
"thyme"],
"Meats":
["chicken",
"beef",
"venison",],
"Spices":
["pepper",
"salt",
"chilli powder",
"cumin"]
}
用户应该能够从下拉列表中选择“罗勒”、“牛肉”、“盐”等,但不能选择“香草”、“肉类”或“香料”,它们只能作为不同成分组的静态标题显示。仅使用tkinter/ttk就可以做到这一点吗?这至少会对您有所帮助
from tkinter import *
root = Tk()
root.title("Tk dropdown example")
mainframe = Frame(root)
mainframe.grid()
tkvar = StringVar(root)
ingredients = {
"Herbs":
["basil",
"oregano",
"thyme"],
"Meats":
["chicken",
"beef",
"venison",],
"Spices":
["pepper",
"salt",
"chilli powder",
"cumin"]
}
c=[]
for k,v in ingredients.items():
c.append(k)
c.extend(v)
ddl = OptionMenu(mainframe, tkvar, *c)
Label(mainframe, text="Choose a dish").grid(row = 1, column = 1)
ddl.grid(row = 2, column =1)
for i in ingredients:
ddl['menu'].entryconfigure(i, state = "disabled",font=('arial italic',11))
root.mainloop()