Open In App

Setting and Retrieving values of Tkinter variable – Python

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Tkinter, control variables are special variables used to link Python values with widgets like Entry, Label and Checkbutton. They act like regular variables but are designed to work with the GUI. These special variables are wrappers around Python data types and can be linked to widget values using .set() and .get() methods. The main types of control variables in Tkinter are:

Tkinter Variable

Python Equivalent

Use Case Example

BooleanVar()

bool

Checkbox status (True/False)

StringVar()

str

Text input/display

IntVar()

int

Integer values

DoubleVar()

float

Floating point numbers

Setting values of Tkinter variables

Let’s explore the different methods available in Tkinter for setting values of these control variables, which help in assigning values and managing the state of widgets effectively.

1. Using the constructor: Variable Constructor lets you set an initial value when creating a variable, like IntVar, StringVar, BooleanVar or DoubleVar. This method immediately assigns the value upon creation, simplifying variable setup. Example:

Python
from tkinter import *

master = Tk()

a = IntVar(master, value=25) # Integer
b = StringVar(master, value="Hello !") # String
c = BooleanVar(master, value=True) # Boolean
d = DoubleVar(master, value=10.25)# Double

Explanation: master = Tk() creates the main window.Then, a, b, c and d are created using IntVar, StringVar, BooleanVar and DoubleVar and initialized with 25, “Hello !”, True and 10.25 respectively.

2. Using set(): It allows you to assign a value to a Tkinter variable after it has been created. This method provides flexibility by letting you modify the value of a variable later in the program. Example:

Python
from tkinter import *

master = Tk()

a = IntVar()
b = StringVar()
c = BooleanVar()
d = DoubleVar()

a.set(100)
b.set("GFG")
c.set(False)
d.set(10.36)

Explanation: master = Tk() creates the main Tkinter window. Variables a, b, c and d are initialized using IntVar, StringVar, BooleanVar and DoubleVar. The set() method is then used to assign values after creation a is set to 100, b to “GFG”, c to False and d to 10.36.

3. Using setvar(): It allows you to set the value of a named variable through the master window in Tkinter. It requires specifying the variable’s name, enabling centralized management of variables via the master widget. This method is useful when you need to modify a variable’s value indirectly through the master object. Example:

Python
from tkinter import *

master = Tk()

a = IntVar(master, name="int")
b = StringVar(master, name="str")
c = BooleanVar(master, name="bool")
d = DoubleVar(master, name="float")

master.setvar(name="int", value=100)
master.setvar(name="str", value="GFG")
master.setvar(name="bool", value=False)
master.setvar(name="float", value=1.236)

Explanation: Variables a, b, c and d are initialized using IntVar, StringVar, BooleanVar, and DoubleVar. IntVar(master, name=”int“) creates an integer variable, StringVar(master, name=”str”) creates a string variable, BooleanVar(master, name=”bool”) creates a boolean variable and DoubleVar(master, name=”float”) creates a floating-point variable, all linked to the master window.

Retrieving values of Tkinter variables

Let’s explore the different methods available in Tkinter for retrieving the values of control variables. These methods allow us to access and work with the data stored in Tkinter variables, helping us manage widget states and interact with the user input effectively.

1. Using get(): It retrieves the current value of a Tkinter variable, such as IntVar, StringVar, BooleanVar or DoubleVar. It is the simplest way to access a variable’s value, which can then be used in the program for display or other operations. Example:

Python
from tkinter import *

master = Tk()

a = IntVar(master, value=100)
b = StringVar(master, value="GFG")
c = BooleanVar(master, value=False)
d = DoubleVar(master, value=1.236)

print(a.get())
print(b.get())
print(c.get())
print(d.get())

Output

100
GFG
False
1.236

Explanation: By using the constructor, the variables a, b, c and d are initialized with specific values. a.get() retrieves the integer 100, b.get() returns the string “GFG”, c.get() gives the boolean False and d.get() returns the float 1.236.

2. Using getvar(): It fetch the value of a variable using its name as a string, directly from the master widget like Tk(). It’s useful when you want to access a variable by name, especially in dynamic scenarios where you may not have a reference to the variable object. Example:

Python
from tkinter import *

master = Tk()

a = IntVar(master, name="int")
b = StringVar(master, name="str")
c = BooleanVar(master, name="bool")
d = DoubleVar(master, name="float")

master.setvar(name="int", value=100)
master.setvar(name="str", value="GFG")
master.setvar(name="bool", value=False)
master.setvar(name="float", value=1.236)

print(master.getvar(name="int"))
print(master.getvar(name="str"))
print(master.getvar(name="bool"))
print(master.getvar(name="float"))

Output

100
GFG
False
1.236

Explanation: By using named constructors, the variables a, b, c and d are linked to the master and assigned values using setvar(). getvar() then retrieves these values, 100 for int, “GFG” for str, False for bool and 1.236 for float.



Next Article
Article Tags :
Practice Tags :

Similar Reads