How to update the command of an OptionMenu

后端 未结 3 1799
无人共我
无人共我 2021-01-23 01:56

I am trying to set or update the command of an OptionMenu after its instantiation.

The widget.configure(command=foo) statement works for Button

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-23 02:34

    I think what you're really asking is how to associate a command to an Optionmenu, rather than update a command (there is no command, so there's nothing to update).

    If you want a function to be called every time a value is selected from an Optionmenu, you can add a trace on the related variable. The trace will call a function whenever that variable changes, whether through the Optionmenu or any other means.

    For example:

    ...
    var = tk.StringVar()
    def foo(*args):
        print "the value changed...", var.get()
    var.trace("w", foo)
    ...
    

    When the function is called it will pass three arguments, which you can safely ignore in this case.

    For more information on variable traces see http://effbot.org/tkinterbook/variable.htm


    You might also want to consider switching to the ttk combobox. It supports binding to <>, which is every so slightly less clunky than doing a variable trace.

提交回复
热议问题