How to Create an On/Off Toggle Switch in Python Tkinter?

I recently faced the challenge of implementing a user-friendly and visually appealing on/off switch in a Tkinter GUI application for one of my clients in New York. In this tutorial, I will explain how to create an On/Off toggle switch in Python Tkinter library. I will share my experience and provide a step-by-step guide.

Tkinter Button Widget

To create an on/off toggle switch, we will utilize the Tkinter Button widget. The Button widget is a graphical control element that allows users to interact with the application by clicking on it. By customizing the appearance and behavior of the Button widget, we can create a toggle switch effect.

Read How to Use Tkinter Entry Widget in Python?

Design the On/Off Toggle Switch

Before getting into the code, let’s consider the design aspects of the on/off toggle switch. The switch should have two states: “On” and “Off,” and it should visually indicate the current state to the user. Additionally, clicking on the switch should toggle between the two states.

To achieve this, we can use the relief property of the Button widget to create a raised or sunken effect, simulating the appearance of a physical switch.

Check out How to Create Radio Buttons in Python with Tkinter?

Implement the On/Off Toggle Switch

Now, let’s proceed with the implementation of the on/off toggle switch in Python Tkinter. Here’s a step-by-step guide:

  1. Import the necessary Tkinter modules:
import tkinter as tk
  1. Create a Tkinter window:
window = tk.Tk()
window.title("On/Off Toggle Switch")
  1. Define a function to handle the toggle functionality:
def toggle_switch():
    if toggle_button.config('relief')[-1] == 'sunken':
        toggle_button.config(relief="raised", bg="SystemButtonFace", text="Off")
    else:
        toggle_button.config(relief="sunken", bg="green", text="On")

In this function, we check the current state of the button using the relief property. If the button is sunken (indicating the “On” state), we change its relief to raised, the background color to the default button face color, and the text to “Off.” Otherwise, we set the relief to sunken, the background color to green, and the text to “On.”

  1. Create the toggle button:
toggle_button = tk.Button(window, text="Off", width=10, command=toggle_switch)
toggle_button.pack(pady=20)

Here, we create a Button widget with the initial text “Off,” a width of 10 characters, and associate it with the toggle_switch function using the command parameter. The button is then packed into the window with a vertical padding of 20 pixels.

  1. Run the Tkinter event loop:
window.mainloop()

This line starts the Tkinter event loop, which handles user interactions and keeps the window responsive.

You can see the output in the screenshot below.

Create an OnOff Toggle Switch in Python Tkinter

Read How to Create and Customize Listboxes in Python Tkinter?

Create an On/Off Toggle Switch in Python Tkinter

Let’s say we’re building a smart home control panel for a client in the United States. We can incorporate the on/off toggle switch to control various devices in the home. Here’s an example:

import tkinter as tk

def toggle_living_room_lights():
    if living_room_button.config('relief')[-1] == 'sunken':
        living_room_button.config(relief="raised", bg="SystemButtonFace", text="Living Room Lights Off")
    else:
        living_room_button.config(relief="sunken", bg="green", text="Living Room Lights On")

def toggle_kitchen_lights():
    if kitchen_button.config('relief')[-1] == 'sunken':
        kitchen_button.config(relief="raised", bg="SystemButtonFace", text="Kitchen Lights Off")
    else:
        kitchen_button.config(relief="sunken", bg="green", text="Kitchen Lights On")

window = tk.Tk()
window.title("Smart Home Control Panel")

living_room_button = tk.Button(window, text="Living Room Lights Off", width=20, command=toggle_living_room_lights)
living_room_button.pack(pady=10)

kitchen_button = tk.Button(window, text="Kitchen Lights Off", width=20, command=toggle_kitchen_lights)
kitchen_button.pack(pady=10)

window.mainloop()

In this example, we create two on/off toggle switches for controlling the living room lights and kitchen lights separately. The toggle_living_room_lights and toggle_kitchen_lights functions handle the toggling of the respective buttons.

You can see the output in the screenshot below.

How to Create an OnOff Toggle Switch in Python Tkinter

Check out Expense Tracking Application Using Python Tkinter

Best Practices and Tips

When implementing on/off toggle switches in your Tkinter application, consider the following best practices and tips:

  • Use descriptive names for your toggle switch functions and variables to enhance code readability.
  • Customize the appearance of the toggle switch to match your application’s design theme. You can modify the colors, fonts, and sizes to create a cohesive look.
  • Consider adding tooltips or labels to provide clear instructions to users on how to interact with the toggle switches.
  • Test your toggle switches thoroughly to ensure they function correctly and provide a smooth user experience.

Read Python Tkinter notebook Widget

Conclusion

In this tutorial, I helped you learn how to create an On/Off toggle switch in Python Tkinter. I discussed how to design and implement the On/Off toggle switch in Python Tkinter. I also discussed an example of creating an On/Off toggle switch and some best practices and tips.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.