tk variables
Variable Binding
import tkinter as tk
# Create the main window
root = tk.Tk()
root.geometry("300x200")
root.title("Variable binding Examples")
# Example 1: Variable Binding
shared_var = tk.StringVar()
entry = tk.Entry(root, font=("Helvetica", 16), justify="left", width=20, textvariable=shared_var)
entry.pack()
label = tk.Label(root, font=("Helvetica", 16), anchor="w", width=20, bg='yellow', textvariable=shared_var)
label.pack()
root.mainloop()
Variables
StringVar
IntVar
DoubleVar
BooleanVar
Binding variables to widgets
Label: Can display text that is linked to a variable.
- textvariable
label = tk.Label(root, textvariable=string_var)
Entry: Can take user input and link it to a variable.
- textvariable
entry = tk.Entry(root, textvariable=string_var)
Checkbutton: Can be linked to a variable to track its state (checked or unchecked).
- variable
checkbutton = tk.Checkbutton(root, variable=int_var)
Radiobutton: Can be linked to a variable to track which radio button is selected.
- variable
radiobutton = tk.Radiobutton(root, variable=int_var, value=1)
Listbox: Can be linked to a variable to track the selected item.
- listvariable
listbox = tk.Listbox(root, listvariable=string_var)
Spinbox: Can be linked to a variable to track the current value.
- textvariable
spinbox = tk.Spinbox(root, textvariable=int_var)
Scale: Can be linked to a variable to track the current value of the scale.
- variable
scale = tk.Scale(root, variable=double_var)
Label
This code uses the textvariable option to set the display text of the label.
import tkinter as tk
root = tk.Tk()
root.title("Label Example")
root.geometry("300x200")
string_var = tk.StringVar()
string_var.set("Hello, Tkinter!")
label = tk.Label(root, textvariable=string_var)
label.pack()
root.mainloop()
Entry
import tkinter as tk
root = tk.Tk()
root.title("Entry Example")
root.geometry("300x200")
string_var = tk.StringVar()
entry = tk.Entry(root, textvariable=string_var)
entry.pack()
def upper_case_text():
string_var.set(string_var.get().upper())
button = tk.Button(root, text="Upper case text", command=upper_case_text)
button.pack()
root.mainloop()
Listbox
string_var = tk.StringVar(value=["Item 1", "Item 2", "Item 3"]) sets the initial value of the StringVar to a list of strings: ["Item 1", "Item 2", "Item 3"]import tkinter as tk
def show_selection():
selected_indices = listbox.curselection()
selected_values = [listbox.get(i) for i in selected_indices]
# Insert the selected values into the text widget
text_widget.delete(1.0, tk.END)
text_widget.insert(tk.END, ", ".join(selected_values))
root = tk.Tk()
root.title("Listbox Example")
root.geometry("300x300")
string_var = tk.StringVar(value=["Item 1", "Item 2", "Item 3"])
listbox = tk.Listbox(root, listvariable=string_var)
listbox.pack()
button = tk.Button(root, text="Show Selection", command=show_selection)
button.pack()
# Create a Text widget to display the selected values
text_widget = tk.Text(root, height=3, width=30)
text_widget.pack()
root.mainloop()
Spinbox
import tkinter as tk
def update_label():
# Update the label with the current value of the Spinbox
label.config(text=f"Current Value: {int_var.get()}")
root = tk.Tk()
root.title("Spinbox Example")
root.geometry("300x200")
# Create an IntVar with an initial value
int_var = tk.IntVar(value=5)
# Create a Spinbox and associate it with the IntVar
spinbox = tk.Spinbox(root, from_=0, to=10, font=("Helvetica", 16), width=5, textvariable=int_var, command=update_label)
spinbox.pack(pady=5)
# Create a Label to display the current value of the Spinbox
label = tk.Label(root, text=f"Current Value: {int_var.get()}", font=("Helvetica", 16))
label.pack(pady=5)
root.mainloop()
import tkinter as tk
def update_label():
# Update the label with the current value of the Spinbox
label.config(text=f"Current Value: {int_var.get()}")
root = tk.Tk()
root.title("Spinbox Example 2")
root.geometry("300x200")
# Create an IntVar with an initial value
int_var = tk.IntVar(value=0)
# Create a Spinbox and associate it with the IntVar
spinbox = tk.Spinbox(root, from_=-10, to=10, increment=2, font=("Helvetica", 16), width=5, textvariable=int_var, command=update_label)
spinbox.pack(pady=5)
# Create a Label to display the current value of the Spinbox
label = tk.Label(root, text=f"Current Value: {int_var.get()}", font=("Helvetica", 16))
label.pack(pady=5)
root.mainloop()
Scale
Defines `update_intensity` function: Updates the rectangle's color intensity based on the Scale value.
Creates a `DoubleVar`: Initializes with a value of 0.5.
Creates a `Scale` widget: Horizontal, ranges from 0 to 1, linked to DoubleVar, calls update_intensity on change.
Creates a `Canvas` widget: to enable displays of a rectangle.
Creates a rectangle on the `Canvas`: Initial color intensity set based on DoubleVar.
The expression {intensity:02x} is a Python string formatting operation that converts an integer to a two-digit hexadecimal string
import tkinter as tk
def update_intensity(value):
# Update the rectangle's intensity based on the Scale value
intensity = int(float(value) * 255)
color = f"#{intensity:02x}{intensity:02x}{intensity:02x}"
canvas.itemconfig(rect, fill=color)
root = tk.Tk()
root.title("Scale Example")
root.geometry("300x200")
# Create a DoubleVar with an initial value
double_var = tk.DoubleVar(value=0.5)
# Create a Scale and associate it with the DoubleVar
scale = tk.Scale(root, from_=0, to=1, resolution=0.01, orient=tk.HORIZONTAL, variable=double_var, command=update_intensity)
scale.pack()
# Create a Canvas to display the greyscale rectangle
canvas = tk.Canvas(root, width=200, height=100)
canvas.pack(pady=20)
# Create a rectangle with initial intensity
initial_intensity = int(double_var.get() * 255)
rect = canvas.create_rectangle(0, 0, 200, 100, fill=f"#{initial_intensity:02x}{initial_intensity:02x}{initial_intensity:02x}")
root.mainloop()