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()
希望您覺得這有幫助。
添加回答
舉報