2 回答
TA贡献1891条经验 获得超3个赞
如果您的意思是创建两个,并且当在第一个下拉菜单中选择不同的值时,它将显示不同的值。你可以试试这个:OptionMenu
import tkinter as tk
from tkinter import ttk
from tkinter import *
def func(selected_value): # the selected_value is the value you selected in the first drop down menu.
dropMenu2.set_menu(*optionList2.get(selected_value))
root = tk.Tk()
root.geometry('500x500')
#Label to Continent
label_1 = tk.Label(root, text="Select the Continent", font = (8), bg = '#ffe1c4')
label_1.place(x = 120, y = 220)
# Continent selection - drop down
optionList1 = ["-","Continent1", "Continent2","Continent3"]
dropVar1 = StringVar()
dropMenu1 = ttk.OptionMenu(root, dropVar1 , *optionList1,command=func) # bind a command for the first dropmenu
dropMenu1.place(x = 300, y = 220)
#Label to Select Country
label_2 = tk.Label(root, text="Select the Country ", font = (8), bg = '#ffe1c4')
label_2.place(x = 120, y = 250)
# Country name selection - drop down
optionList2 = { # when select different value,show the list.
"Continent1": ["Country_11", "Country_12"],
"Continent2": ["Country_21", "Country_22"],
"Continent3": ["Country_31", "Country_32"]
}
dropVar2 = StringVar()
dropMenu2 = ttk.OptionMenu(root, dropVar2, "-")
dropMenu2.place(x = 300, y = 250)
root.mainloop()
现在是:enter image description here
选择其他值时:enter image description here
(一个建议:比 更漂亮,使用不是一个好习惯。ttk.ComboboxOptionMenufrom tkinter import *
TA贡献1862条经验 获得超7个赞
如果你的意思是菜单里面的菜单,那么这是可能的,而且非常简单,因为中使用的菜单是一个金特,请参阅tkinter菜单的文档。OptionMenu()Menu()
我们可以访问类似的Menu
Op = OptionMenu(root, var, 'Hello', 'HI', 'YOO')
# Op_Menu is the Menu() class used for OptionMenu
Op_Menu = Op['menu']
下面是选项菜单中嵌套菜单的一个小示例。当您选择任何大陆内的任何国家/地区时,选项菜单的文本不会更改,因此要修复我使用的参数,并且在国家/地区的每个命令参数中,我正在更改分配给选项菜单的值。commandStringVar
import tkinter as tk
root = tk.Tk()
svar = tk.StringVar()
svar.set('Antarctica')
Op = tk.OptionMenu(root, svar, svar.get())
OpMenu = Op['menu']
Op.pack()
Menu1 = tk.Menu(OpMenu)
OpMenu.add_cascade(label='Africa', menu= Menu1)
Menu1.add_command(label='Algeria', command=lambda: svar.set('Africa - Algeria'))
Menu1.add_command(label='Benin', command=lambda: svar.set('Africa - Benin'))
Menu2 = tk.Menu(Op['menu'])
OpMenu.add_cascade(label='Asia', menu= Menu2)
Menu2.add_command(label='China', command=lambda: svar.set('Asia - China'))
Menu2.add_command(label='India', command=lambda: svar.set('Asia - India'))
root.mainloop()
希望您觉得这有帮助。
添加回答
举报