The Wayback Machine - https://web.archive.org/web/20240618021453/https://www.geeksforgeeks.org/on-off-toggle-button-switch-in-tkinter/
Open In App

On/Off Toggle Button Switch in Tkinter

Last Updated : 19 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Tkinter

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications.

In this article, we will learn how to make an on/off switch using Tkinter.

Approach:

  • Make a global variable named as is_on; the default value is True, which indicates the switch is ON.
  • Make two Image Objects; one object has “on image” and another one has “off image”.
  • Create a button that has “on image” as default; set the border width to be zero.
  • Make a function that will change the button image, using the config() method.
  • The function will check, whether the is_on value is True or False

Image link:

Below is the Implementation:

Python3




# Import Module
from tkinter import *
 
# Create Object
root = Tk()
 
# Add Title
root.title('On/Off Switch!')
 
# Add Geometry
root.geometry("500x300")
 
# Keep track of the button state on/off
#global is_on
is_on = True
 
# Create Label
my_label = Label(root,
    text = "The Switch Is On!",
    fg = "green",
    font = ("Helvetica", 32))
 
my_label.pack(pady = 20)
 
# Define our switch function
def switch():
    global is_on
     
    # Determine is on or off
    if is_on:
        on_button.config(image = off)
        my_label.config(text = "The Switch is Off",
                        fg = "grey")
        is_on = False
    else:
       
        on_button.config(image = on)
        my_label.config(text = "The Switch is On", fg = "green")
        is_on = True
 
# Define Our Images
on = PhotoImage(file = "on.png")
off = PhotoImage(file = "off.png")
 
# Create A Button
on_button = Button(root, image = on, bd = 0,
                   command = switch)
on_button.pack(pady = 50)
 
# Execute Tkinter
root.mainloop()


Output:



Previous Article
Next Article

Similar Reads

Tkinter Application to Switch Between Different Page Frames
Prerequisites: Python GUI – tkinter Sometimes it happens that we need to create an application with several pops up dialog boxes, i.e Page Frames. Here is a step by step process to create multiple Tkinter Page Frames and link them! This can be used as a boilerplate for more complex python GUI applications like creating interfaces for Virtual Labora
6 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Python | Toggle button in kivy using .kv file
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Toggle button: The ToggleButton widget acts like a checkbox. Wh
3 min read
PyQt5 - Toggle Button
In PyQt5 a toggle button is basically the push button in a special state. Push button is a button when we press it do some task and get back to the normal state it is similar to key board key when we press it, it do some thing and when we release it, it comes back to its original form. ToggleButton is a type of push button, but it has two states i.
2 min read
Change color of button in Python - Tkinter
Prerequisite: Creating a button in tkinter, Python GUI – tkinter In this article, we are going to write a Python script to change the color of the button in Tkinter. It can be done with two methods: Using bg properties.Using activebackground properties. Example 1: using bg properties. We can change the button background color with bg properties, Th
1 min read
Python | Add style to tkinter button
Tkinter is a Python standard library that is used to create GUI (Graphical User Interface) applications. It is one of the most commonly used packages of Python. Tkinter supports both traditional and modern graphics support with the help of Tk themed widgets. All the widgets that Tkinter also has available in tkinter.ttk.Adding style in a tkinter.tt
4 min read
Python | Add image on a Tkinter button
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive. Image can be added with the help of PhotoImage() method. This is a Tkinter method
3 min read
How to check which Button was clicked in Tkinter ?
Are you using various buttons in your app, and you are being confused about which button is being pressed? Don't know how to get rid of this solution!! Don't worry, just go through the article. In this article, we will explain in detail the procedure to know which button was pressed. Steps by Step Approach: Step 1: First, import the library Tkinter
3 min read
Open a new Window with a button in Python-Tkinter
Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.Note: For more informa
3 min read
Tkinter - Button that changes its properties on hover
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using tkinter is an easy task.
2 min read
Practice Tags :