Tkinter supports some variables which are used to manipulate the values of Tkinter widgets. These variables work like normal variables.
set() and get() methods are used to set and retrieve the values of these variables.
The values of these variables can be set using set() method or by using constructor of these variables.
There are 4 tkinter variables.
- BooleanVar()
- StringVar()
- IntVar()
- DoubleVar()
Setting values of Tkinter variables –
1. Using variable’s constructor
Syntax:
var = Tkinter_variable(master, value = any_value)
# importing tkinter module from tkinter import * # creating Tk() variable # required by Tkinter classes master = Tk() # Tkinter variables # initialization using constructor intvar = IntVar(master, value = 25, name ="2") strvar = StringVar(master, "Hello !") boolvar = BooleanVar(master, True) doublevar = DoubleVar(master, 10.25) |
2. Using set() method
Syntax:
var = Tkinter_variable(master=None) var.set(value)
# importing tkinter module from tkinter import * # creating Tk() variable # required by Tkinter classes master = Tk() # Declaration of Tkinter variables intvar = IntVar() strvar = StringVar() boolvar = BooleanVar() doublevar = DoubleVar() # Initialization of Tkinter variables # using set() method intvar.set(100) strvar.set("GFG") boolvar.set(False) doublevar.set(10.36) |
3. Using setvar() method
Syntax:
var = Tkinter_variable(master = None, name = "NAME") master.setvar(name="NAME", value = any_value) NOTE: When uisng this method name parameter is required (mandatory).
# importing tkinter module from tkinter import * # creating Tk() variable # required by Tkinter classes master = Tk() # Tkinter variables # Giving user defined names to each variables # so that variables can be modified easily intvar = IntVar(master, name ="int") strvar = StringVar(master, name ="str") boolvar = BooleanVar(master, name ="bool") doublevar = DoubleVar(master, name ="float") # Setting values of variables # using setvar() method 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) |
Retrieving values of tkinter variables –
1. Using get() method
Syntax:
tkinter_variable.get()
# importing tkinter module from tkinter import * # creating Tk() variable # required by Tkinter classes master = Tk() # Tkinter variables # Giving user defined names to each variables # so that variables can be modified easily intvar = IntVar(master, name ="int") strvar = StringVar(master, name ="str") boolvar = BooleanVar(master, name ="bool") doublevar = DoubleVar(master, name ="float") # Setting values of variables # using setvar() method 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) # getting values of each variables using get() method print("Value of IntVar()", intvar.get()) print("Value of StringVar()", strvar.get()) print("Value of BooleanVar()", boolvar.get()) print("Value of DoubleVar()", doublevar.get()) |
2. Using getvar() method
Syntax:
var = Tkinter_variable(master, name = "NAME") master.getvar(name = "NAME") NOTE: When uisng this method name parameter is required (mandatory).
# importing tkinter module from tkinter import * # creating Tk() variable # required by Tkinter classes master = Tk() # Tkinter variables # Giving user defined names to each variables # so that variables can be modified easily intvar = IntVar(master, name ="int") strvar = StringVar(master, name ="str") boolvar = BooleanVar(master, name ="bool") doublevar = DoubleVar(master, name ="float") # Setting values of variables # using setvar() method 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) # getting values of each variables using getvar() method print("Value of IntVar()", master.getvar(name ="int")) print("Value of StringVar()", master.getvar(name ="str")) print("Value of BooleanVar()", master.getvar(name ="bool")) print("Value of DoubleVar()", master.getvar(name ="float")) |
Value of IntVar() 100 Value of StringVar() GFG Value of BooleanVar() False Value of DoubleVar() 1.236
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Retrieving And Updating Data Contained in Shelve in Python
- Python IMDbPY – Retrieving person using person ID
- Python IMDbPY – Retrieving company using company ID
- Python IMDbPY - Retrieving movie using movie ID
- Python IMDbPY – Retrieving art department cast from the movie object
- Python IMDbPY - Retrieving actor from the movie details
- Python IMDbPY - Retrieving role played by actor from the movie details
- Retrieving Cookies in Python
- Retrieving File Data From HDFS using Python Snakebite
- PyQt5 - Retrieving caption of Check Box
- PyQt5 Scrollable Label - Retrieving tooltip text
- PyQt5 QSpinBox - Retrieving Description
- Retrieving HTML From data using Flask
- Setting the position of TKinter labels
- Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
- Python Tkinter | Create LabelFrame and add widgets to it
- Getting screen's height and width using Tkinter | Python
- Python | forget_pack() and forget_grid() method in Tkinter
- Python | winfo_ismapped() and winfo_exist() in Tkinter
- Python Tkinter | grid_location() and grid_size() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

