我们可以在tkinter中嵌套两个OptionMenu小部件吗?

3 投票
2 回答
592 浏览
提问于 2025-06-08 05:50

就像一个经典的例子,我们有一个下拉框显示洲的名称,选择某个洲后,另一个下拉框会显示与这个洲相关的国家名称。我们怎么用Tkinter来实现这个呢?

我在第一个下拉框里放了洲的列表,第二个下拉框里放了所有与这些洲相关的国家。我希望当选择洲_1时,能显示国家_11和国家_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 个回答

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() 

希望你觉得这个有帮助。

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()

现在的效果是这样的:

这里输入图片描述

当选择另一个值时,效果是这样的:

这里输入图片描述

(一个建议:ttk.ComboboxOptionMenu 看起来更好,而且使用 from tkinter import * 不是一个好的做法。)

撰写回答