Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a Tkinter toggle button?
Python's Tkinter library provides a comprehensive set of widgets for creating GUI applications. A toggle button is a special type of button that switches between two states when clicked, commonly used for on/off functionality like enabling night mode or day mode in applications.
To create a toggle button in Tkinter, we use a regular Button widget with custom images and a function that changes the button's state and appearance when clicked. The key is using a global variable to track the current state.
Creating a Basic Toggle Button
Here's how to create a toggle button that switches between day and night modes ?
# Import tkinter library
from tkinter import *
# Create main window
win = Tk()
win.title('Toggle Button Demonstration')
win.geometry("700x400")
win.resizable(0, 0)
# Global variable to track button state
is_on = True
# Create label to display current mode
label = Label(win, text="Night Mode is On", bg="white", fg="black",
font=("Poppins bold", 22))
label.pack(pady=20)
# Toggle function
def button_mode():
global is_on
# Switch between states
if is_on:
on_button.config(image=off_image)
label.config(text="Day Mode is On", bg="white", fg="black")
is_on = False
else:
on_button.config(image=on_image)
label.config(text="Night Mode is On", fg="black")
is_on = True
# Load toggle button images (requires on.png and off.png files)
on_image = PhotoImage(file="on.png")
off_image = PhotoImage(file="off.png")
# Create toggle button
on_button = Button(win, image=on_image, bd=0, command=button_mode)
on_button.pack(pady=50)
# Run the application
win.mainloop()
How It Works
The toggle button works through these key components ?
-
Global State Variable:
is_ontracks whether the button is currently in the "on" or "off" state -
Toggle Function:
button_mode()switches between states and updates the button image and label text -
PhotoImage Objects:
on_imageandoff_imageprovide visual feedback for different states -
Button Widget: Uses the
commandparameter to call the toggle function when clicked
Alternative: Text-Based Toggle Button
If you don't have image files, you can create a text-based toggle button ?
import tkinter as tk
def create_text_toggle():
root = tk.Tk()
root.title("Text Toggle Button")
root.geometry("300x200")
# State variable
is_active = tk.BooleanVar(value=False)
# Toggle function
def toggle():
current = is_active.get()
is_active.set(not current)
if is_active.get():
toggle_btn.config(text="ON", bg="green", fg="white")
status_label.config(text="Status: Active")
else:
toggle_btn.config(text="OFF", bg="red", fg="white")
status_label.config(text="Status: Inactive")
# Create widgets
toggle_btn = tk.Button(root, text="OFF", bg="red", fg="white",
font=("Arial", 12, "bold"), command=toggle)
toggle_btn.pack(pady=20)
status_label = tk.Label(root, text="Status: Inactive", font=("Arial", 10))
status_label.pack(pady=10)
root.mainloop()
create_text_toggle()
# Creates a window with a red "OFF" button that turns green "ON" when clicked
Key Points
- Use a global variable or
BooleanVar()to track the toggle state - PhotoImage objects work well for custom toggle button designs
- The
config()method updates button properties dynamically - Consider using text and color changes if images aren't available
- Always provide visual feedback to show the current state
Conclusion
Creating toggle buttons in Tkinter involves using a regular Button widget with a state-tracking variable and a toggle function. Whether using images or text-based approaches, the key is providing clear visual feedback for the current state.
---