📜  Python|设置和检索 Tkinter 变量的值

📅  最后修改于: 2022-05-13 01:55:46.348000             🧑  作者: Mango

Python|设置和检索 Tkinter 变量的值

Tkinter 支持一些用于操作 Tkinter 小部件值的变量。这些变量像普通变量一样工作。
set() 和 get() 方法用于设置和检索这些变量的值。
可以使用 set() 方法或使用这些变量的构造函数来设置这些变量的值。
有 4 个 tkinter 变量。

  • 布尔变量()
  • 字符串变量()
  • 整数变量()
  • 双变量()

设置 Tkinter 变量的值 –

1.使用变量的构造函数
句法:

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)


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)


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)


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


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


2. 使用 set() 方法
句法:

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. 使用 setvar() 方法
句法:

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)

检索 tkinter 变量的值 –

1.使用get()方法
句法:

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.使用getvar()方法
句法:

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