Getting the choice of optionmenu right after selection Python

后端 未结 2 1302
暖寄归人
暖寄归人 2021-02-06 17:44

I was wondering if there is any way for me to see what the User has selected among the list displaying, let\'s say: [\"Apple\",\"Orange\",\"Grapes\"] right after th

2条回答
  •  遥遥无期
    2021-02-06 18:10

    Trace the StringVar.

    from Tkinter import *
    
    def option_changed(*args):
        print "the user chose the value {}".format(variable.get())
        print a
    
    master = Tk()
    
    a = "Foo"
    variable = StringVar(master)
    variable.set("Apple") # default value
    variable.trace("w", option_changed)
    
    w = OptionMenu(master, variable, "Apple", "Orange", "Grapes")
    w.pack()
    
    mainloop()
    

    Here, option_changed will be called whenever the user chooses an option from the option menu.


    You can wrap the trace argument in a lambda to specify your own parameters.

    def option_changed(foo, bar, baz):
        #do stuff
    
    #...
    variable.trace("w", lambda *args: option_changed(qux, 23, "hello"))
    

提交回复
热议问题