能否在tkinter中嵌套两个OptionMenu小部件?

3

就像有一个下拉菜单中的大洲名称,显示与所选大洲相关的国家名称一样。如何使用Tkinter实现此目标?我在第一个下拉菜单中有大洲列表,以及所有与大洲相关的国家列表。当选择大洲1时,我想显示country_11,country_12等国家,对于其他大洲也是如此。

以下是我正在处理的代码片段 -

import tkinter as tk
from tkinter import ttk
from tkinter import *

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)
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 = ["Country_11", "Country_12", "Country_21","Country_22","Country_31","Country_32"]
dropVar2 = StringVar()
dropMenu2 = ttk.OptionMenu(root, dropVar2, *optionList2)
dropMenu2.place(x = 300, y = 250)

root.mainloop()

希望有解决方案,因为我不知道Tkinter中的OptionMenu可以具有哪些属性。谢谢!
2个回答

4
如果您想创建两个 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.Combobox比OptionMenu更漂亮,使用from tkinter import *不是一个好的实践。)

2
如果你指的是菜单中的菜单,那么这是可能的,而且非常简单,因为在OptionMenu()中使用的菜单是tkinter的Menu(),请参见 tkinter Menu文档
我们可以像这样访问Menu
Op = OptionMenu(root, var, 'Hello', 'HI', 'YOO')

# Op_Menu is the Menu() class used for OptionMenu
Op_Menu = Op['menu']

这是一个选项菜单中嵌套菜单的小例子。当您选择任何一个大洲内的国家时,选项菜单的文本不会改变,为了解决这个问题,我使用了 command 参数,在每个国家的命令参数中我都改变了分配给选项菜单的 StringVar 的值。
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() 

希望你会觉得这个很有帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接