19

我目前正在尝试制作两个OptionMenus,其中第二个将根据第一个中选择的内容动态更新OptionMenu

例如,我想OptionMenu_A用 list

[North America, Europe, Asia]
  • 如果Asia选中,OptionMenu_B则将更改为类似[Japan, China, Malasia].
  • 如果Europe被选中,那么它将变为[Germany, France, Switzerland]例如。

我可以制作两个OptionMenus 但无法OptionMenu_B根据OptionMenu_As 的状态进行更新。

有人会好心地证明这样的事情是否可能吗?

4

2 回答 2

27

对的,这是可能的。StringVar.trace您可以检查第一个选项何时更改。然后删除第二个 OptionMenu 的所有选项,并用相应的选项填充它。如果你有一个像字典这样的数据结构,那么映射对应关系会很容易:

import sys
if sys.version_info[0] >= 3:
    import tkinter as tk
else:
    import Tkinter as tk


class App(tk.Frame):

    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
                     'Europe': ['Germany', 'France', 'Switzerland']}

        self.variable_a = tk.StringVar(self)
        self.variable_b = tk.StringVar(self)

        self.variable_a.trace('w', self.update_options)

        self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys())
        self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '')

        self.variable_a.set('Asia')

        self.optionmenu_a.pack()
        self.optionmenu_b.pack()
        self.pack()


    def update_options(self, *args):
        countries = self.dict[self.variable_a.get()]
        self.variable_b.set(countries[0])

        menu = self.optionmenu_b['menu']
        menu.delete(0, 'end')

        for country in countries:
            menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation))


if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    app.mainloop()
于 2013-06-22T15:35:43.450 回答
2

嗨 只是补充一点。我设法制作了 3 个动态选项菜单,其中两个从上一个下拉菜单中更新...

from tkinter import *
root = Tk()

def update_options_B(*args):
    countries = data[variable_a.get()]
    variable_b.set(countries[0])
    menu = optionmenu_b['menu']
    menu.delete(0, 'end')
    for country in countries:
        menu.add_command(label=country, command=lambda nation=country: variable_b.set(nation))

def update_options_C(*args):
    cities = data2[variable_b.get()]
    variable_c.set(cities[0])
    menu = optionmenu_c['menu']
    menu.delete(0, "end")
    for city in cities:
        menu.add_command(label=city, command=lambda nation=city: variable_c.set(nation))


data = {'Asia': ['Japan', 'China', 'Malasia'],'Europe': ['Germany', 'France', 'Switzerland'], 'Africa': ['Nigeria', 'Kenya', 'Ethiopia']}
data2 = {'Japan': ["jiustu", "kamikaz", "Tokyo"], 'China': ["Shaigon", "Hong Kong"], 'Malasia': ["tiramusto", "quala lopour"], 'Germany': ["Dusseldorf", "Berlin", "Hambourg"], 'France': ["Paris", "Lille"], 'Switzerland': ["Biern", "Bonn"], 'Nigeria': ['Nigeria1', 'Nigeria3'], 'Kenya': ["Keny West", "Notorious"], 'Ethiopia': ["Etanpi", "Neeandertaal"]}
variable_a = StringVar()
variable_b = StringVar()
variable_c = StringVar()

variable_a.trace('w', update_options_B)
variable_b.trace('w', update_options_C)
optionmenu_a = OptionMenu(root, variable_a, *data.keys())
optionmenu_b = OptionMenu(root, variable_b, '')
optionmenu_c = OptionMenu(root, variable_c, '')

variable_a.set('Asia')
optionmenu_a.pack()
optionmenu_b.pack()
optionmenu_c.pack()


root.mainloop()
于 2020-08-09T06:10:11.080 回答