The Wayback Machine - https://web.archive.org/web/20240718064343/https://www.geeksforgeeks.org/python-setting-and-retrieving-values-of-tkinter-variable/
Open In App

Python | setting and retrieving values of Tkinter variable

Last Updated : 16 Aug, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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)

Python3




# 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)

Python3




# 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 using this method name parameter is required (mandatory).

Python3




# 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()

Python3




# 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 using this method name parameter is required (mandatory).

Python3




# 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"))


Output: 

Value of IntVar() 100
Value of StringVar() GFG
Value of BooleanVar() False
Value of DoubleVar() 1.236

 



Previous Article
Next Article

Similar Reads

Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Retrieving And Updating Data Contained in Shelve in Python
In Python shelve you access the keys randomly. In order to access the keys randomly in python shelve we use open() function. This function works a lot like the file open() function in File handling. Syntax for open the file using Python shelve shelve.open(filename, flag='c' , writeback=True) In Order to access the keys randomly in shelve in Python,
3 min read
Python IMDbPY – Retrieving person using person ID
In this article we will see how we can retrieve the data of person using its person ID, person id is the unique id given to each person by IMDb. We can use search_person method to search the persons by their name but it gives many people as they have same names therefore retrieving a person by its id is a better option. In order to search a person
1 min read
Python IMDbPY – Retrieving company using company ID
In this article we will see how we can retrieve the data of company using its company ID, company id is the unique id given to each company by IMDb. We can use search_company method to search the companies by their name but it gives many companies as they have same names therefore retrieving a company by its id is a better option. In order to searc
1 min read
Python IMDbPY - Retrieving movie using movie ID
In this article we will see how we can retrieve the data of movie using its movie ID, movie id is the unique id given to each movie by IMDb. We can use search_movie method to search the movies by their name but it gives many movies as they have same names therefore retrieving a movie by its id is a better option. In order to search a movie by its i
1 min read
Python IMDbPY – Retrieving art department cast from the movie object
In this article we will see how we can retrieve the art department cast that involved in the movie from the movie information, movie id is the unique id given to each movie by IMDb. We can use search_movie method to search the movies by their name but it gives many movies as they have same names therefore retrieving a movie by its id is a better op
2 min read
Python IMDbPY - Retrieving actor from the movie details
In this article we will see how we can retrieve the actors name performed in the movie from the movie information, movie id is the unique id given to each movie by IMDb. We can use search_movie method to search the movies by their name but it gives many movies as they have same names therefore retrieving a movie by its id is a better option. In ord
2 min read
Python IMDbPY - Retrieving role played by actor from the movie details
In this article we will see how we can retrieve the role played by the actor in a given movie from the movie information, movie id is the unique id given to each movie by IMDb. We can use search_movie method to search the movies by their name but it gives many movies as they have same names therefore retrieving a movie by its id is a better option.
2 min read
Retrieving Cookies in Python
Retrieving cookies in Python can be done by the use of the Requests library. Requests library is one of the integral part of Python for making HTTP requests to a specified URL. The below codes show different approaches to do show: 1. By requesting a session: # import the requests library import requests # initialize a session session = requests.Ses
1 min read
Retrieving File Data From HDFS using Python Snakebite
Prerequisite: Hadoop Installation, HDFS Python Snakebite is a very popular Python library that we can use to communicate with the HDFS. Using the Python client library provided by the Snakebite package we can easily write Python code that works on HDFS. It uses protobuf messages to communicate directly with the NameNode. The python client library d
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg