Python Tkinter On-Off Switch

In this tutorial, we will learn How to create a Python Tkinter On-Off Switch and we will also cover different examples related to the on-off switch. And, we will cover these topics

  • Python Tkinter simple toggle switch
  • Python Tkinter on-off slide switch
  • Python Tkinter on-off using Push button

Python Tkinter simple toggle switch

In this tutorial, we will learn how to create a Simple Toggle switch in Python Tkinter.

The switch is used to make the connection in an electric circuit. We here use the switch as a feature that we can use in any gaming application or any similar application to turn on or to turn off that function.

Code:

In the following code, we import the Tkinter library import tkinter as tk and also create a widget in which we use a simple toggle switch. Here toggle switch is working like a normal switch which we used in our daily life to have access to switch on the light of the house.

Button() is used as a normal toggle switch to turn on or to turn off that function.

from tkinter import *

def Simpletoggle():
    
    if toggle_button.config('text')[-1] == 'ON':
        toggle_button.config(text='OFF')
    else:
        toggle_button.config(text='ON')

ws = Tk()
ws.title("Python Guides")
ws.geometry("200x100")

toggle_button = Button(text="OFF", width=10, command=Simpletoggle)
toggle_button.pack(pady=10)

ws.mainloop()

Output:

After running the following code, we get the following output in which we see when we click on them the toggle switch will be ON. And when we click on them again, the toggle switch will get OFF.

Python Tkinter simple toggle switch
Simple Toddle Switch Output

Read: Python Tkinter Events

Python Tkinter on-off slide switch

In this section, we will learn how to create On-Off slide switch in Python Tkinter.

We are looking for the technology as it is getting better day by day and physical working has taken over by the AI/ML. The same use of the on-off slide switch which we make can be used in mobile apps with IoT embedded applications. To switch on lights, bulbs, fans, camera, AC, etc.

Code:

In the following code, we create a widget in which an on-off slide switch is placed. We use the label, button and also add photo images to show how the on-off button work.

  • Label() is used to display fields of text or images.
  • Button() is used as a normal switch to turn on or to turn off that function.
  • Photoimage() is used to add the image in the widget.

from tkinter import *
  

ws = Tk()
  
ws.title('Python Guides')
  
ws.geometry("400x300")
  
#global is_on
is_on = True
  
label = Label(ws, 
    text = "The Switch Is On!", 
    fg = "green", 
    font = ("Helvetica", 32))
  
label.pack(pady = 20)
  
def Switch():
    global is_on
      
    if is_on:
        button.config(image = off)
        label.config(text = "Switch is Off", 
                        fg = "grey")
        is_on = False
    else:
        
        button.config(image = on)
        label.config(text = "Switch is On", fg = "green")
        is_on = True
  
on = PhotoImage(file = "on.png")
off = PhotoImage(file = "off.png")
  
button = Button(ws, image = on, bd = 0,
                   command = Switch)
button.pack(pady = 50)
  
ws.mainloop()

Output:

In the following output, we see if the user slides the switch on the right side the switch automatically gets ON. On other hand, the user slides the switch on the left side switch automatically gets off.

Python Tkinter On off switch 1
On-Off slide switch Output

Read: Python Tkinter notebook Widget

Python Tkinter on-off using Push button

In this section, we will learn how to create an on-off Push button in Python Tkinter.

We here use the On-Off push button as a feature that we can use in any gaming application or any similar application to turn on or to turn off that function. Here button is working like a normal switch which we used in our daily life to have access to switch on the light of the house.

Code:

In the following code, we use def convert() to define the function and also use the state method to disable or enable the connection.

Button() is used as a normal switch to turn on or to turn off that function.

from tkinter import *
ws = Tk()
ws.title("Python Guides")

def convert():
    if(a1['state']==NORMAL):
        a1["state"] = DISABLED
        a2["text"]="ON"
    elif (a1['state']==DISABLED):
        a1["state"]=NORMAL
        a2["text"]="OFF"


a1=Button(ws, text="Bulb")
a1.config(height = 8, width = 9)
a1.grid(row=0, column=0)    
a2 = Button(text="OFF", command=convert)
a2.grid(row=0,column=1)
ws.mainloop()

Output:

In the above code, first, we have created a button object “a1” and then, we are using the IF statement to check the state of the button. In the end, we are using the state to change the behavior of the button to get the desired result.

After running the following code, we get the following output in which we see when we click on them the button will be ON. And when we click on them again, the button will get OFF.

Python Tkinter On Off button
On-Off Pushbutton Output

You may also like to read the following Tkinter tutorials.

So, in this tutorial, we discuss the Python Tkinter On-Off switch and we also covered different examples. Here is the list of examples that we have covered.

  • Python Tkinter simple toggle
  • Python Tkinter on-off slide switch
  • Python Tkinter on-off using Push button