user7238416
user7238416

Reputation:

Tkinter OptionMenu, How to call diffrent func on each option

am doing a GUI which includes option menu I have defined the following menu to take the options from a list, however I would like to make the each option call a different function, but I couldn't figure out how to do it.

    options = ["Modem ID: 20",
                "Modem ID: 30",
                "Modem ID: 40", 
                "Modem ID: 50"]

    selected_Option
    selected_Option = tk.StringVar(self) 
    selected_Option.set(options[0])
    drop_Menu =apply(OptionMenu, (self, selected_Option) + tuple(options))
    drop_Menu.place(relx=.809, y=5)  

Upvotes: 1

Views: 742

Answers (1)

acw1668
acw1668

Reputation: 46678

You can use .trace(..) function on StringVar to monitor the change of the value, and then do whatever you want based on the selected option:

def onOptionChanged(*args):
    modem_id = int(selected_Option.get().split(':')[1])
    if modem_id == 10:
        # do something
    elif modem_id == 20:
        # do other stuff
    ...

...
selected_Option = StringVar()
selected_Option.trace('w', onOptionChanged)
...

Upvotes: 1

Related Questions