variable
, textvariable
,
onvalue
, offvalue
, and value
. This
connection works both ways: if the variable changes for any reason,
the widget it's connected to will be updated to reflect the new value.
Unfortunately, in the current implementation of Tkinter it is not
possible to hand over an arbitrary Python variable to a widget through
a variable
or textvariable
option. The only
kinds of variables for which this works are variables that are
subclassed from a class called Variable, defined in the Tkinter
module.
There are many useful subclasses of Variable already defined:
StringVar
, IntVar
, DoubleVar
, and
BooleanVar
. To read the current value of such a variable,
say, one called myval, you call the get() method on it, and to change
its value you call the set() method. If you follow this protocol, the
widget will always track the value of the variable, with no further
intervention on your part.
For example: