On/Off Toggle Button Switch in Tkinter Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue 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: Switch On Switch Off 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: https://media.geeksforgeeks.org/wp-content/uploads/20210117181116/FreeOnlineScreenRecorderProject8.mp4 Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve. Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 19 Oct, 2021 Like Article Save Article Previous Temperature Converter using Tkinter Next Inter Thread Communication With Condition() Method in Python Share your thoughts in the comments Add Your Comment Please Login to comment...