Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
In tkinter
, there are special variable classes designed to hold specific types of values, like StringVar
, IntVar
, DoubleVar
, and BooleanVar
. These variable classes provide special methods for setting, retrieving, and tracing changes to their values. This functionality is often used in tandem with widgets like Entry
, Label
, Checkbutton
, and Radiobutton
, which require a way to keep track of their state or content.
1. Import Required Libraries:
import tkinter as tk
2. Create the Main Application Window:
root = tk.Tk() root.title("Tkinter Variables")
3. Create and Set a tkinter
Variable:
Here's how you can create different types of tkinter
variables and set their values:
# String variable name_var = tk.StringVar() name_var.set("John Doe") # Integer variable age_var = tk.IntVar() age_var.set(25) # Double variable (for floating point values) score_var = tk.DoubleVar() score_var.set(85.5) # Boolean variable active_var = tk.BooleanVar() active_var.set(True)
4. Retrieving Values from tkinter
Variables:
You can retrieve the current value held by a tkinter
variable using the get()
method:
name = name_var.get() age = age_var.get() score = score_var.get() is_active = active_var.get() print(f"Name: {name}, Age: {age}, Score: {score}, Active: {is_active}")
5. Using tkinter
Variables with Widgets:
These variables can be used with various widgets to automatically update the widget's value when the variable changes and vice versa:
# Using with Entry widget name_entry = tk.Entry(root, textvariable=name_var) name_entry.pack(pady=10) # Using with Label widget age_label = tk.Label(root, textvariable=age_var) age_label.pack(pady=10) # Update a variable and see the change on the widget name_var.set("Alice Smith")
6. Run the Main Loop:
root.mainloop()
Complete Code:
import tkinter as tk root = tk.Tk() root.title("Tkinter Variables") # Create and set variables name_var = tk.StringVar() name_var.set("John Doe") age_var = tk.IntVar() age_var.set(25) score_var = tk.DoubleVar() score_var.set(85.5) active_var = tk.BooleanVar() active_var.set(True) # Retrieve values name = name_var.get() age = age_var.get() score = score_var.get() is_active = active_var.get() print(f"Name: {name}, Age: {age}, Score: {score}, Active: {is_active}") # Use with widgets name_entry = tk.Entry(root, textvariable=name_var) name_entry.pack(pady=10) age_label = tk.Label(root, textvariable=age_var) age_label.pack(pady=10) name_var.set("Alice Smith") root.mainloop()
tkinter
variables are incredibly powerful because they can be tied directly to widget values, allowing for automatic updates in real-time without needing explicit event handling or callbacks for every change.
How to assign values to Tkinter variables:
import tkinter as tk root = tk.Tk() # StringVar str_var = tk.StringVar() str_var.set("Hello, Tkinter!") # IntVar int_var = tk.IntVar() int_var.set(42) # DoubleVar double_var = tk.DoubleVar() double_var.set(3.14) root.mainloop()
Tkinter variable assignment and retrieval:
import tkinter as tk root = tk.Tk() # StringVar str_var = tk.StringVar() str_var.set("Hello, Tkinter!") str_value = str_var.get() print(f"StringVar Value: {str_value}") # IntVar int_var = tk.IntVar() int_var.set(42) int_value = int_var.get() print(f"IntVar Value: {int_value}") # DoubleVar double_var = tk.DoubleVar() double_var.set(3.14) double_value = double_var.get() print(f"DoubleVar Value: {double_value}") root.mainloop()
Getting and setting values of Tkinter StringVar:
import tkinter as tk root = tk.Tk() str_var = tk.StringVar() def update_value(): current_value = str_var.get() print(f"Current Value: {current_value}") # Entry widget to set StringVar value entry = tk.Entry(root, textvariable=str_var) entry.pack() # Button to trigger value retrieval btn_get_value = tk.Button(root, text="Get Value", command=update_value) btn_get_value.pack() root.mainloop()
Updating Tkinter variable values dynamically:
import tkinter as tk def update_variable(): current_value = int_var.get() int_var.set(current_value + 1) root = tk.Tk() int_var = tk.IntVar() int_var.set(0) label = tk.Label(root, textvariable=int_var) label.pack() btn_update = tk.Button(root, text="Update", command=update_variable) btn_update.pack() root.mainloop()
Retrieving values from Tkinter variable in Python:
import tkinter as tk root = tk.Tk() str_var = tk.StringVar() str_var.set("Hello, Tkinter!") retrieved_value = str_var.get() print(f"Retrieved Value: {retrieved_value}") root.mainloop()
Managing values in Tkinter IntVar, DoubleVar, StringVar, etc.:
import tkinter as tk root = tk.Tk() int_var = tk.IntVar() int_var.set(42) double_var = tk.DoubleVar() double_var.set(3.14) str_var = tk.StringVar() str_var.set("Hello, Tkinter!") # Use these variables in widgets as needed root.mainloop()
Setting and getting values with Tkinter variable classes:
import tkinter as tk root = tk.Tk() # StringVar str_var = tk.StringVar() str_var.set("Hello, Tkinter!") str_value = str_var.get() # IntVar int_var = tk.IntVar() int_var.set(42) int_value = int_var.get() # DoubleVar double_var = tk.DoubleVar() double_var.set(3.14) double_value = double_var.get() # Use these values in widgets or other parts of the program root.mainloop()