3

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.

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.

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


EDIT:

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

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

MoTSCHIGGE
  • 949
  • 5
  • 12
  • 21

1 Answers1

8

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

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))
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685