Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Tkinter Variables and Methods


Let's explore Tkinter variables and methods step by step. Tkinter provides several variable classes that can be used to manage the values of widgets dynamically. We'll cover StringVar, IntVar, DoubleVar, and BooleanVar, as well as some common methods for interacting with these variables.


1. Tkinter Variable Classes

Tkinter provides variable classes to manage widget values dynamically:

  • StringVar: For string variables.
  • IntVar: For integer variables.
  • DoubleVar: For floating-point variables.
  • BooleanVar: For boolean variables.

2. Using StringVar

StringVar is used to manage text values in widgets such as Entry, Label, etc.

Example:


python
                        import tkinter as tk

                        def update_label(*args):
                            label.config(text=entry_var.get())

                        # Create the main window
                        root = tk.Tk()
                        root.title("StringVar Example")
                        root.geometry("300x200")

                        # Create a StringVar
                        entry_var = tk.StringVar()

                        # Create an Entry widget
                        entry = tk.Entry(root, textvariable=entry_var)
                        entry.pack(pady=20)

                        # Create a Label widget
                        label = tk.Label(root, text="Type something above")
                        label.pack(pady=10)

                        # Trace the variable to update the label when entry changes
                        entry_var.trace_add("write", update_label)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • entry_var = tk.StringVar(): Creates a StringVar instance.
  • textvariable=entry_var: Links the StringVar to the Entry widget.
  • entry_var.trace_add("write", update_label): Traces changes to the variable and calls the update_label function when the variable's value changes.

3. Using IntVar

IntVar is used to manage integer values in widgets such as Spinbox, Scale, etc.

Example:


python
                        import tkinter as tk

                        def update_label(*args):
                            label.config(text=f"Value: {spinbox_var.get()}")

                        # Create the main window
                        root = tk.Tk()
                        root.title("IntVar Example")
                        root.geometry("300x200")

                        # Create an IntVar
                        spinbox_var = tk.IntVar()

                        # Create a Spinbox widget
                        spinbox = tk.Spinbox(root, from_=0, to=10, textvariable=spinbox_var)
                        spinbox.pack(pady=20)

                        # Create a Label widget
                        label = tk.Label(root, text="Value: 0")
                        label.pack(pady=10)

                        # Trace the variable to update the label when spinbox changes
                        spinbox_var.trace_add("write", update_label)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • spinbox_var = tk.IntVar(): Creates an IntVar instance.
  • textvariable=spinbox_var: Links the IntVar to the Spinbox widget.
  • spinbox_var.trace_add("write", update_label): Traces changes to the variable and calls the update_label function when the variable's value changes.


4. Using DoubleVar

DoubleVar is used to manage floating-point values in widgets such as Scale, etc.

Example:


python
                        import tkinter as tk

                        def update_label(*args):
                            label.config(text=f"Value: {scale_var.get():.2f}")

                        # Create the main window
                        root = tk.Tk()
                        root.title("DoubleVar Example")
                        root.geometry("300x200")

                        # Create a DoubleVar
                        scale_var = tk.DoubleVar()

                        # Create a Scale widget
                        scale = tk.Scale(root, from_=0.0, to=1.0, orient="horizontal", resolution=0.01, variable=scale_var)
                        scale.pack(pady=20)

                        # Create a Label widget
                        label = tk.Label(root, text="Value: 0.00")
                        label.pack(pady=10)

                        # Trace the variable to update the label when scale changes
                        scale_var.trace_add("write", update_label)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • scale_var = tk.DoubleVar(): Creates a DoubleVar instance.
  • variable=scale_var: Links the DoubleVar to the Scale widget.
  • scale_var.trace_add("write", update_label): Traces changes to the variable and calls the update_label function when the variable's value changes.


5. Using BooleanVar

BooleanVar is used to manage boolean values in widgets such as Checkbutton, etc.

Example:


python
                        import tkinter as tk

                        def update_label(*args):
                            label.config(text=f"Checked: {check_var.get()}")

                        # Create the main window
                        root = tk.Tk()
                        root.title("BooleanVar Example")
                        root.geometry("300x200")

                        # Create a BooleanVar
                        check_var = tk.BooleanVar()

                        # Create a Checkbutton widget
                        checkbutton = tk.Checkbutton(root, text="Check me", variable=check_var)
                        checkbutton.pack(pady=20)

                        # Create a Label widget
                        label = tk.Label(root, text="Checked: False")
                        label.pack(pady=10)

                        # Trace the variable to update the label when checkbutton changes
                        check_var.trace_add("write", update_label)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • check_var = tk.BooleanVar(): Creates a BooleanVar instance.
  • variable=check_var: Links the BooleanVar to the Checkbutton widget.
  • check_var.trace_add("write", update_label): Traces changes to the variable and calls the update_label function when the variable's value changes.


6. Common Methods for Variables

Tkinter variables (StringVar, IntVar, DoubleVar, BooleanVar) provide common methods for getting and setting their values.

  • get(): Returns the current value of the variable.
  • set(value): Sets the variable to a new value.
  • trace_add(mode, callback): Adds a callback to be executed when the variable changes.
  • trace_remove(mode, callback): Removes the callback.

Example: Using get() and set() Methods


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Get and Set Example")
                        root.geometry("300x200")

                        # Create a StringVar
                        entry_var = tk.StringVar()

                        # Create an Entry widget
                        entry = tk.Entry(root, textvariable=entry_var)
                        entry.pack(pady=20)

                        # Create a Label widget
                        label = tk.Label(root, text="Type something above")
                        label.pack(pady=10)

                        # Button to show the current value
                        def show_value():
                            label.config(text=f"Current value: {entry_var.get()}")

                        button_show = tk.Button(root, text="Show Value", command=show_value)
                        button_show.pack(pady=5)

                        # Button to set a new value
                        def set_value():
                            entry_var.set("New Value")

                        button_set = tk.Button(root, text="Set Value", command=set_value)
                        button_set.pack(pady=5)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • entry_var.get(): Gets the current value of entry_var.
  • entry_var.set("New Value"): Sets the value of entry_var to "New Value".

Summary

Tkinter variables (StringVar, IntVar, DoubleVar, BooleanVar) are powerful tools for managing widget values dynamically. They provide methods like get(), set(), and trace_add() to facilitate interaction and automatic updates between widgets and variables. Using these variable classes can help you create responsive and interactive Tkinter applications.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java