亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我們可以在 tkinter 中嵌套兩個選項菜單小部件嗎?

我們可以在 tkinter 中嵌套兩個選項菜單小部件嗎?

慕少森 2022-09-27 16:41:42
就像一個著名的例子一樣,在一個下拉列表中包含大洲名稱,并顯示與所選大洲相關的國家/地區名稱。如何使用特金特實現這一點?我在第一個下拉列表中有大陸列表,并在列表中列出了與大陸相關的所有國家/地區。我想在選擇continent_1時顯示country_11,country_12,對于其他大陸也是如此。這是我正在處理的一段代碼 -import tkinter as tkfrom tkinter import ttkfrom 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 downoptionList1 = ["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 downoptionList2 = ["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中可以具有的所有屬性。提前致謝??!
查看完整描述

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 *


查看完整回答
反對 回復 2022-09-27
?
牧羊人nacy

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

希望您覺得這有幫助。


查看完整回答
反對 回復 2022-09-27
  • 2 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號