+

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                                        
             
                
            
            
        

    



                    




            
            
            
Get Solution for free from DataCamp guru

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