[Tutor] Event handler for optionmenu

geon geon at post.cz
Mon Aug 15 11:41:30 CEST 2005

Jorge Louis De Castro napsal(a):

> Hi,
> I have an optionmenu widget that works just fine except that  can't 
> find docs to get hold of events as they happen, kinda like the 
> command=XYZ of other widgets like buttons.
> The code I'm using for the option menu is the following:
> OPTIONS = ["en","pt","es","it","fr","de"]
> self.startw.variable = StringVar()
> self.startw.variable.set(OPTIONS[0]) # default value
> self.startw.whis_butt=apply(OptionMenu, (self.startw, 
> self.startw.variable) + tuple(OPTIONS))
> self.startw.whis_butt.grid(column=1, row=3)
> Now I'd like to have a function like:
> def onChange(self):
>      # do optionmenu specific stuff
> but can't find how to do it, how to associate/bind onChange with the 
> optionmenu widget.
> Any help will be highly appreciated.

This art is called Trace Variable and could be used like this (copied 
and rearranged from one of the prevously posted message from here):

from Tkinter import *


def callbackFunc(name, index, mode):
  #print "callback called with name=%r, index=%r, mode=%r" % (name, 
index, mode)
  varValue = root.getvar(name)
  print varValue
  # modify the value, just to show it can be done
  #root.setvar(name, varValue)

root = Tk()
var = StringVar()
var.set(OPTIONS[2]) # default value
rememberMe = var.trace_variable('w', callbackFunc)  
# var.trace('w', callbackFunc) # this works, too

w = OptionMenu (root, var, *OPTIONS)


Hope its what you asked for :-)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050815/e35fe061/attachment.htm

More information about the Tutor mailing list