4. Variable Classes in Tkinter
By Bernd Klein. Last modified: 16 Dec 2021.
Variable Classes
Some widgets (like text entry widgets, radio buttons and so on) can be connected directly to application variables by using special options: 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. These Tkinter control variables are used like regular Python variables to keep certain values.
It's not possible to hand over a regular 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. They are declared like this:
- x = StringVar() # Holds a string; default value ""
- x = IntVar() # Holds an integer; default value 0
- x = DoubleVar() # Holds a float; default value 0.0
- x = BooleanVar() # Holds a boolean, returns 0 for False and 1 for True
To read the current value of such a variable, call the method get(). The value of such a variable can be changed with the set() method.
Live Python training
Upcoming online Courses
10 Mar to 14 Mar 2025
07 Apr to 11 Apr 2025
23 Jun to 27 Jun 2025
28 Jul to 01 Aug 2025
Efficient Data Analysis with Pandas
10 Mar to 11 Mar 2025
07 Apr to 08 Apr 2025
02 Jun to 03 Jun 2025
23 Jun to 24 Jun 2025
28 Jul to 29 Jul 2025
09 Apr to 11 Apr 2025
04 Jun to 06 Jun 2025
Python and Machine Learning Course
10 Mar to 14 Mar 2025
07 Apr to 11 Apr 2025
02 Jun to 06 Jun 2025
28 Jul to 01 Aug 2025
See our Python training courses