TKinter IntVar() documentation

Fredrik Lundh effbot at telia.com
Wed Aug 30 03:15:27 EDT 2000


Benjamin wrote:
> How does IntVar() works? It is not documented, so I tried to use it by
> myself but it doesn't work well.
> 
> I've defined one for a checkbutton.
> I want to connect thet value of IntVar() to a callback using trace
> 
> v = IntVar()
> v.trace("rw",callback)
> 
> def callback(event):
>     do things
> 
> burt it doesn't works, I have an error message every time I dor v.set(x) or
> v.get() "3 arguments, 1 argument expected"

the callback is called with three arguments; the variable name,
the variable index (if the variable is a Tcl array), and an event
flag ("r" for read, "w" for write, "u" for delete).

this should work:

    def callback(name, index, op):
        ...

:::

note that the variable name is a string containing the Tcl-level
name of the variable, not the Python name.  it's usually some-
thing like "PY_VAR12".

the index is always empty if you use the standard wrappers
provided by Python (they're all scalars).

:::

Tkinter doesn't maintain a mapping from Tcl variable names to
Python objects, so if you want to use the same callback for
several variables, you're in for some extra work:

-- one way is to use lambda wrappers, just like you can do
with event handlers:

    def callback(variable):
        print variable.get()

    v.trace("w", lambda *args, v=v: callback(v))

-- another way is to maintain your own variable dictionary,
mapping str(variable) (which gives the Tcl name) to variable
instances:

    def callback(name, index, op):
        print vardict[name].get()

    v = IntVar()
    vardict[str(v)] = v
    v.trace("w", callback)

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->




More information about the Python-list mailing list