[Tutor] Tkinter event for changing OptionMenu items

geon geon at post.cz
Thu Jul 21 21:59:46 CEST 2005


Bernard Lebel wrote:

>Hello,
>
>I'm trying to bind an event to the changes made to an OptionMenu. Ie
>the user chooses a different item, the rest of the Tk window gets
>updated. To repopulate the window, a function would be called by the
>binding.
>
>Any suggestion?
>
>
>var1 = StringVar()
>var1.set( aTables[0] )
>oOptionMenu = OptionMenu( oRoot, var1, aTables[0], *aTables[1:] )
>sTableName = var1.get()
>oOptionMenu.bind( '<Return>', getTableColumns )
>oOptionMenu.pack( fill = X )
>  
>
I might know what you mean - binding optionMenu with say 
w.bind('<ButtonPress-1>', tisk) is "too early", it is called not after 
the choice, but at the beginning, at clicking on the button.

I found "curious" solution, here it is: (maybe it could be done 
otherwise, I would like to know, too..):

from Tkinter import *

OPTIONS = [
    "egg",
    "bunny",
    "chicken"
]

def callmeagain():
    global state
   
    if state!=var.get():
        print var.get()
        state=var.get()
    root.after(1000, callmeagain)   

root = Tk()
var = StringVar()
var.set(OPTIONS[2]) # default value
state=var.get()

w = OptionMenu (root, var, *OPTIONS)
w.pack()

callmeagain()
root.mainloop()

Maybe this could help, too.

-- 
geon
Vyjímka je pravidlo. Rekurzivní.



More information about the Tutor mailing list