Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a little bit unclear what you mean by "pass parameters to my callback function." You're already doing that! For example: </p> <pre><code>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() </code></pre> <p>When run...</p> <pre><code>$ python foo.py ('PY_VAR0', '', 'w') {} Hello World! </code></pre> <p>So you see, when Tkinter calls your callback, it passes parameters to it. If you wanted to do something other than print them, you could store them in some state, by passing a method instead of a function. </p> <pre><code>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() </code></pre> <p>When run...</p> <pre><code>$ python foo.py [('PY_VAR0', '', 'w')] [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] </code></pre> <p>Also, perhaps you want to access the value of <code>optionvalue</code>. You could save a reference to it then: </p> <pre><code>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() </code></pre> <p>When run...</p> <pre><code>$ python foo.py [('PY_VAR0', '', 'w')] 1 [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] 2 [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')] 3 </code></pre> <p>You could also use <code>root.getvar(name)</code> with <code>name = 'PY_VAR0'</code> (the first arg passed to the callback), as <a href="https://stackoverflow.com/a/9869270/577088">noob oddy</a> suggests.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload