Tkinter GUI:根据另一个选项菜单中的选择更新选项菜单中的选择 [英] Tkinter GUI: Update choices of an option menu depending on a choice from another option menu

查看:152
本文介绍了Tkinter GUI:根据另一个选项菜单中的选择更新选项菜单中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个由多个下拉菜单组成的Tkinter-GUI,这些菜单提供了一些功能的参数,这些功能可以通过点击GUI按钮来运行。
问题是此函数只能用几个参数组合执行(并非所有组合都是可行的)。因此,我希望GUI根据从第一个下拉菜单中进行的选择自动更新其下拉菜单中的选项列表。

I built a Tkinter-GUI consisting of several dropdown menus that provide some parameters for a function that can be run by hitting a GUI button. The problem is that this function can only be executed with a few parameter combinations (not every combination is possible). Thus I want the GUI to automatically update the list of choices inside of its dropdown menus depending on the choice from the first dropdown menu.

我有一个函数可以导出在选项A设置为特定值的情况下,选项B,C,D,E的可能参数值,但我不知道如何更新GUI显示的选项。

I have a function that can derive the possible parameter values for the options B,C,D,E under the condition that the option A is set to a certain value but I don´t know how to update the choices that are displayed by the GUI.

是否可以使用Tkinter函数实现GUI选项菜单中选项的自动更新?

Is there a Tkinter function that I can use to realize an automatic update of the choices in the GUI option menus?

编辑:

这是我尝试过的脚本。它似乎充满了错误,但我认为它可以说明我想做什么:

Here is the script I tried. It seems to be full of bugs but I think it can illustrate what I want to do:

choices4option1 = [1,2,3]
choices4option2 = [4,5,6]
choices4option3 = [7,8,9]
def update_GUI_choices(option):
    if option == 'option1':
        if GUI_options.option1.get()==1:
           global choices4option2       
           choices4option2 = [4,5]
        else: pass
    elif option == 'option2':
        if GUI_options.option2.get()==4:
           global choices4option3       
           choices4option3 = [8,9]
        else: pass
    else: pass

# GUI
import Tkinter as tk
GUI = tk.Tk()

class GUI_options:
    option1 = tk.IntVar(GUI)
    option1.set('choose a value')
    option2 = tk.IntVar(GUI)
    option2.set('choose a value')
    option3 = tk.IntVar(GUI)
    option3.set('choose a value')

om_option1 = tk.OptionMenu(GUI, GUI_options.option1, *choices4option1)
om_option1.grid(column=0, row=0)
menu_option1 = om_option1.children["menu"]
menu_option1.delete(0,'end')
for values in (choices4option1):
    menu_option1.add_command(label=values, command=lambda: update_GUI_choices('option1'))

om_option2 = tk.OptionMenu(GUI, GUI_options.option2, *choices4option2)
om_option2.grid(column=0, row=1)
menu_option2 = om_option2.children["menu"]
menu_option2.delete(0,'end')
for values in (choices4option2):
    menu_option1.add_command(label=values, command=lambda: update_GUI_choices('option2'))

om_option3 = tk.OptionMenu(GUI, GUI_options.option3, *choices4option3)
om_option3.grid(column=0, row=2)
menu_option3 = om_option3.children["menu"]
menu_option3.delete(0,'end')
for values in (choices4option3):
    menu_option3.add_command(label=values, command=lambda: update_GUI_choices('option3'))

GUI.mainloop()

完成菜单1中的选择后,如何提示选项菜单2获得新值?

How can I prompt the option menu 2 to get new values when a choice in menu 1 is done?

推荐答案

选项菜单由两部分组成:按钮和菜单。您可以从小部件的 children属性中获取这些部分。

An optionmenu is made up of two parts: a button and a menu. You can get these parts from the "children" attribute of the widget.

om = tk.OptionMenu(...)
menu = om.children["menu"]

您可以删除所有旧值在具有标准菜单功能的菜单中:

You can delete all of the old values in the menu with standard menu functions:

menu.delete(0, "end")

您可以使用标准菜单功能向菜单添加新项目。您需要将每个命令与设置关联变量的命令相关联。因此,例如,如果您知道与选项菜单关联的变量名为 self.choice,则可以执行以下操作:

You can add new items to the menu with standard menu functions. You need to associate a command with each that sets the associated variable. So, for example, if you know the variable associated with the option menu is named "self.choice", you can do something like this:

for value in ("value1", "value2", ...):
    menu.add_command(label=value, command=lambda v=value: self.choice.set(v))

这篇关于Tkinter GUI:根据另一个选项菜单中的选择更新选项菜单中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆