Tkinter admite algunas variables que se utilizan para manipular los valores de los widgets de Tkinter. Estas variables funcionan como variables normales.
Los métodos set() y get() se utilizan para establecer y recuperar los valores de estas variables.
Los valores de estas variables se pueden establecer usando el método set() o usando el constructor de estas variables.
Hay 4 variables tkinter.
- Var booleana()
- VarString()
- IntVar()
- DobleVar()
Configuración de valores de variables Tkinter –
1. Usando
la sintaxis del constructor de variables:
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. Usando el método set()
Sintaxis:
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. Usando el método setvar()
Sintaxis:
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)
Recuperando valores de variables tkinter –
1. Usando el método get()
Sintaxis:
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. Usando el método getvar()
Sintaxis:
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"))
Value of IntVar() 100 Value of StringVar() GFG Value of BooleanVar() False Value of DoubleVar() 1.236
Publicación traducida automáticamente
Artículo escrito por sanjeev2552 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA