Python设置和检索Tkinter变量的值

本文概述

  • 设置Tkinter变量的值–
  • 检索tkinter变量的值–
Tkinter支持一些变量, 这些变量用于操纵Tkinter小部件的值。这些变量像普通变量一样工作。
Set()和get()方法用于设置和检索这些变量的值。
这些变量的值可以使用
可以使用set()方法或使用这些变量的构造函数来设置这些变量的值。
有4个tkinter变量。
  • BooleanVar()
  • StringVar()
  • IntVar()
  • DoubleVar()
设置Tkinter变量的值– 1.使用变量的构造函数
语法如下:
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课程。

    推荐阅读