Re: [Tutor] Tkinter event for changing OptionMenu items
On Thu, 21 Jul 2005 14:16:05 -0400 Bernard Lebel <[EMAIL PROTECTED]> wrote: > Hi Michael, > > Let say I have a MenuOption, that consists of 3 items. This MenuOption > sits on top of the Tkinter window. > > In the lower part, I have a bunch of widgets (text fields). When the > choose a different item from the MenuOption, it would call a function > that clears the lower part and repopulates it with new fields. My > problem is binding this callback to the event of choosing an item. > > In this case I would try the StringVar()'s trace method, which you can use to track changes of its value; a simple example: >>> from Tkinter import * >>> root = Tk() >>> s = StringVar() >>> s.set('a') >>> om = OptionMenu(root, s, 'a', 'b', 'c', 'd') >>> om.pack() >>> def changed(*args): ... print s.get() ... >>> s.trace('w', changed) In the example changed() is called each time you select an item in the menu and with s.get() you can query the current selection and update the window according to the current value. I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter event for changing OptionMenu items
Quoting Bernard Lebel <[EMAIL PROTECTED]>: > 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? The easiest way is to use a Pmw.OptionMenu, which has this functionality built-in :-) Alternatively, you can trace the StringVar you connect to the OptionMenu. I have never done anything with traces, but there are some instructions here: http://www.astro.washington.edu/owen/ROTKFolklore.html -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter event for changing OptionMenu items
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( '', getTableColumns ) >oOptionMenu.pack( fill = X ) > > I might know what you mean - binding optionMenu with say w.bind('', 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í. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter event for changing OptionMenu items
Hi Michael, Let say I have a MenuOption, that consists of 3 items. This MenuOption sits on top of the Tkinter window. In the lower part, I have a bunch of widgets (text fields). When the choose a different item from the MenuOption, it would call a function that clears the lower part and repopulates it with new fields. My problem is binding this callback to the event of choosing an item. Bernard On 7/21/05, Michael Lange <[EMAIL PROTECTED]> wrote: > On Thu, 21 Jul 2005 12:19:00 -0400 > Bernard Lebel <[EMAIL PROTECTED]> 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( '', getTableColumns ) > > oOptionMenu.pack( fill = X ) > > > > Here the '' event is obviously not the one I'm looking for, > > but is there to illustrate what code I have so far. > > > > > > Hi Bernard, > > if I understand you correctly, the "" event of the OptionMenu's Menu > may be the best > bet for you (not very much tested though): > > oOptionMenu['menu'].bind('', getTableColumns) > > Otherwise you would probably have to call > oOptionMenu['menu'].entryconfigure(command=...) on all menu entries. > > I hope this helps > > Michael > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter event for changing OptionMenu items
On Thu, 21 Jul 2005 12:19:00 -0400 Bernard Lebel <[EMAIL PROTECTED]> 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( '', getTableColumns ) > oOptionMenu.pack( fill = X ) > > Here the '' event is obviously not the one I'm looking for, > but is there to illustrate what code I have so far. > > Hi Bernard, if I understand you correctly, the "" event of the OptionMenu's Menu may be the best bet for you (not very much tested though): oOptionMenu['menu'].bind('', getTableColumns) Otherwise you would probably have to call oOptionMenu['menu'].entryconfigure(command=...) on all menu entries. I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor