Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Tkinter | Adding style to the input text using ttk.Entry widget
Next article icon

In Tkinter,control variables are special variables used to linkPython values with widgets like Entry, Label and Checkbutton. They act like regular variables but are designed to work with the GUI. These special variables are wrappers around Python data types and can be linked to widget values using .set() and .get() methods. The main types of control variables in Tkinter are:

Tkinter Variable

Python Equivalent

Use Case Example

BooleanVar()

bool

Checkbox status (True/False)

StringVar()

str

Text input/display

IntVar()

int

Integer values

DoubleVar()

float

Floating point numbers

Setting values of Tkinter variables

Let’s explore the different methods available in Tkinter for setting values of these control variables, which help in assigning values and managing the state of widgets effectively.

1. Using the constructor: Variable Constructor lets you set an initial value when creating a variable, like IntVar, StringVar, BooleanVar or DoubleVar. This method immediately assigns the value upon creation, simplifying variable setup.Example:

Python
fromtkinterimport*master=Tk()a=IntVar(master,value=25)# Integerb=StringVar(master,value="Hello !")# Stringc=BooleanVar(master,value=True)# Booleand=DoubleVar(master,value=10.25)# Double

Explanation: master = Tk()creates the main window.Then,a,b,c andd are created using IntVar, StringVar, BooleanVar and DoubleVar and initialized with 25, "Hello !", True and 10.25 respectively.

2. Using set(): It allows you to assign a value to a Tkinter variable after it has been created. This method provides flexibility by letting you modify the value of a variable later in the program.Example:

Python
fromtkinterimport*master=Tk()a=IntVar()b=StringVar()c=BooleanVar()d=DoubleVar()a.set(100)b.set("GFG")c.set(False)d.set(10.36)

Explanation: master = Tk()creates the main Tkinter window. Variablesa,b,c andd are initialized using IntVar, StringVar, BooleanVar and DoubleVar. The set() method is then used to assign values after creationa is set to 100,b to "GFG", c to False andd to 10.36.

3. Using setvar():It allows you to set the value of a named variable through the master window in Tkinter. It requires specifying the variable's name, enabling centralized management of variables via the master widget. This method is useful when you need to modify a variable's value indirectly through the master object.Example:

Python
fromtkinterimport*master=Tk()a=IntVar(master,name="int")b=StringVar(master,name="str")c=BooleanVar(master,name="bool")d=DoubleVar(master,name="float")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)

Explanation: Variablesa,b, c and d are initialized using IntVar, StringVar, BooleanVar, and DoubleVar. IntVar(master, name="int") creates an integer variable,StringVar(master, name="str") creates a string variable,BooleanVar(master, name="bool") creates a boolean variable and DoubleVar(master, name="float") creates a floating-point variable, all linked to the master window.

Retrieving values of Tkinter variables

Let’s explore the different methods available in Tkinter for retrieving the values of control variables. These methods allow us to access and work with the data stored in Tkinter variables, helping us manage widget states and interact with the user input effectively.

1. Using get(): It retrieves the current value of a Tkinter variable, such as IntVar, StringVar, BooleanVar or DoubleVar. It is the simplest way to access a variable’s value, which can then be used in the program for display or other operations.Example:

Python
fromtkinterimport*master=Tk()a=IntVar(master,value=100)b=StringVar(master,value="GFG")c=BooleanVar(master,value=False)d=DoubleVar(master,value=1.236)print(a.get())print(b.get())print(c.get())print(d.get())

Output

100
GFG
False
1.236

Explanation: By using the constructor, the variablesa,b, c andd are initialized with specific values.a.get() retrieves the integer 100, b.get() returns the string "GFG",c.get()gives the boolean False andd.get() returns the float 1.236.

2. Using getvar():Itfetch the value of a variable using its name as a string, directly from the master widget likeTk(). It's useful when you want to access a variable by name, especially in dynamic scenarios where you may not have a reference to the variable object.Example:

Python
fromtkinterimport*master=Tk()a=IntVar(master,name="int")b=StringVar(master,name="str")c=BooleanVar(master,name="bool")d=DoubleVar(master,name="float")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)print(master.getvar(name="int"))print(master.getvar(name="str"))print(master.getvar(name="bool"))print(master.getvar(name="float"))

Output

100
GFG
False
1.236

Explanation:By using named constructors, the variablesa,b,candd are linked to the master and assigned values using setvar().getvar()then retrieves these values, 100 for int, "GFG" for str, False for bool and 1.236 for float.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp