Change language

Python | setting and getting values ​​of a Tkinter variable

There are 4 tkinter variables.

  • BooleanVar ()
  • STRINGVAR ()
  • IntVar ()
  • DoubleVar ( )

Setting Tkinter Variables —

1. Using Variable Constructor

Syntax :

 var = Tkinter_variable (master, value = any_value) 

# import tkinter module

from tkinter import *

 
# create a Tk () variable
# required by Tkinter classes

master = Tk ()

  
# Tkinter Variables
# constructor initialization

intvar = IntVar (master, value = 25 , name = "2" )

strvar = StringVar (master, "Hello!" )

boolvar = BooleanVar (master, True )

doublevar = DoubleVar (master, 10.25 )

2. Using the set () method

Syntax :

 var = Tkinter_variable (master = None) var.set (value) 

# import tkinter module

from tkinter import *

  
# create a Tk () variable
# required by Tkinter classes

master = Tk ()

 
# Declaring Tkinter variables

intvar = IntVar ()

strvar = StringVar ()

boolvar = BooleanVar ()

doublevar = DoubleVar ()

 
# Initializing Tkinter variables
# using the 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). 

Getting the values ​​of tkinter variables —

1. Using get () method

Syntax :

 tkinter_variable.get () 

# import tkinter module

from tkinter import *

  
# create a Tk () variable
# required by Tkinter classes

master = Tk ()

 
# Tkinter Variables
# Provide custom names for each variable
# so the variables can be easily changed

intvar = IntVar (master, name = " int " )

strvar = StringVar (master, name = "str" ​​ )

boolvar = BooleanVar (master, name = "bool " )

doublevar = DoubleVar (master, name = "float" )

 
# Setting variable values ​​
# using the 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 )

# import tkinter module

from tkinter import *

 
# create a Tk () variable
# required by Tkinter classes

master = Tk ()

 
# Tkinter Variables
# Provide custom names for each variable
# so that the variables can be easily changed

in tvar = IntVar (master, name = "int" )

strvar = StringVar (master, name = "str" )

boolvar = BooleanVar (master, name = "bool" )

doublevar = DoubleVar (master, name = "float" )

 
# Setting variable values ​​
# using the setva method r ()

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 the values ​​of each variable using the 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). 

Exit:

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

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# import tkinter module

from tkinter import *

  
# create Tk () variable
# required by Tkinter classes

master = Tk ()

 
# Tkinter Variables
# Provide custom names for each variable
# so the variables can be easily changed

intvar = IntVar (master, name = " int " )

strvar = StringVar (master, name = "str" ​​ )

boolvar = BooleanVar (master, name = "bool " )

doublevar = DoubleVar (master, name = "float" )

 
# Setting variable values ​​
# using the 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 the values ​​of each variable with n using the 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 = < / code> "float" ))