It's a bit unclear what you mean by "passing parameters to my callback function". You already do it! For instance:
from Tkinter import * def callbackfunc(*args, **kwargs): print args, kwargs print "Hello World!" class App(object): def __init__(self, master): frame = Frame(master) frame.pack() optionvalue = IntVar(master) optionvalue.set(2) optionvalue.trace("w", callbackfunc) self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4) self.optionmenu.pack() root = Tk() app = App(root) root.mainloop()
At startup ...
$ python foo.py ('PY_VAR0', '', 'w') {} Hello World!
So, you see, when Tkinter calls your callback, it passes parameters to it. If you want to do something other than print them, you can save them in some state by passing a method instead of a function.
from Tkinter import * class App(object): def __init__(self, master): frame = Frame(master) frame.pack() optionvalue = IntVar(master) optionvalue.set(2) optionvalue.trace("w", self.callbackfunc) self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4) self.optionmenu.pack() self.state = [] def callbackfunc(self, *args): self.state.append(args) print self.state root = Tk() app = App(root) root.mainloop()
At startup ...
$ python foo.py [('PY_VAR0', '', 'w')] [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')]
Also, maybe you want to access the value of optionvalue . Then you can save a link to it:
from Tkinter import * class App(object): def __init__(self, master): frame = Frame(master) frame.pack() self.optionvalue = IntVar(master) self.optionvalue.set(2) self.optionvalue.trace("w", self.callbackfunc) self.optionmenu = OptionMenu(master, self.optionvalue, 1, 2, 3, 4) self.optionmenu.pack() self.state = [] def callbackfunc(self, *args): self.state.append(args) print self.state print self.optionvalue.get() root = Tk() app = App(root) root.mainloop()
At startup ...
$ python foo.py [('PY_VAR0', '', 'w')] 1 [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] 2 [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] 3
You can also use root.getvar(name) with name = 'PY_VAR0' (the first argument passed to the callback), as noob oddy suggests.