本文概述
- 设置Tkinter变量的值–
- 检索tkinter变量的值–
Set()和get()方法用于设置和检索这些变量的值。
这些变量的值可以使用
可以使用set()方法或使用这些变量的构造函数来设置这些变量的值。
有4个tkinter变量。
- BooleanVar()
- StringVar()
- IntVar()
- DoubleVar()
语法如下:
var = Tkinter_variable(master, value = http://www.srcmini.com/any_value)
# importing tkinter module
from tkinter import *# creating Tk() variable
# required by Tkinter classes
master = Tk()# Tkinter variables
# initialization using constructor
intvar = IntVar(master, value = http://www.srcmini.com/25 , name ="2" )
strvar = StringVar(master, "Hello !" )
boolvar = BooleanVar(master, True )
doublevar = DoubleVar(master, 10.25 )
2.使用set()方法
语法如下:
var = Tkinter_variable(master=None)
var.set(value)
# 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()方法
【Python设置和检索Tkinter变量的值】语法如下:
var = Tkinter_variable(master = None, name = "NAME")
master.setvar(name="NAME", value = http://www.srcmini.com/any_value)NOTE: When uisng this method name parameter is required (mandatory).
# 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 = http://www.srcmini.com/100 )
master.setvar(name ="str" , value = "http://www.srcmini.com/GFG" )
master.setvar(name = "bool" , value = http://www.srcmini.com/False )
master.setvar(name ="float" , value = http://www.srcmini.com/1.236 )
检索tkinter变量的值– 1.使用get()方法
语法如下:
tkinter_variable.get()
# 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 = http://www.srcmini.com/100 )
master.setvar(name ="str" , value = "http://www.srcmini.com/GFG" )
master.setvar(name = "bool" , value = http://www.srcmini.com/False )
master.setvar(name ="float" , value = http://www.srcmini.com/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 uisng this method name parameter is required (mandatory).
# 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 = http://www.srcmini.com/100 )
master.setvar(name ="str" , value = "http://www.srcmini.com/GFG" )
master.setvar(name = "bool" , value = http://www.srcmini.com/False )
master.setvar(name ="float" , value = http://www.srcmini.com/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
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python集合操作(联合,交集,差分和对称差分)
- Python集合update()函数用法示例
- Python使用Tkinter开发简单GUI计算器
- Python Kivy中的滑块小部件用法示例
- Python使用.kv文件的滑块小部件
- Python使用OpenCV进行微笑检测示例
- Python根据元素的长度对列表进行排序
- Python Kivy中的微调小部件用法示例
- 开源demo| 你画我猜——让你的生活更有趣