

# https://www.globalsino.com/ICs/page4853.html
# On/Off Switch

from tkinter import *

root = Tk()
root.title('On/Off Switch')
root.geometry('500x350')

# Track the button state
global is_off
is_off = True

my_label = Label(root, text = "", fg="grey", font=("Helvetica", 30))
my_label.pack(pady=2)

# Define switch function
def Switch_func():
    global is_off
    # Determine is on or off
    if is_off:
        Off_button.config(image=On_img)
        is_off = False
        my_label.config(text="Switch is on", fg='green')
        print(is_off)
    else:
        Off_button.config(image=Off_img)
        is_off = True
        my_label.config(text="Switch is off", fg='black')
        print(is_off)

#on/off images
On_img = PhotoImage(file=r"C:\GlobalSino2\ICs\images\4536_on.png")
Off_img = PhotoImage(file=r"C:\GlobalSino2\ICs\images\4536_off.png")

# Create a button
Off_button = Button(root, image=Off_img, bd=0, command = Switch_func)
Off_button.pack(pady=50)

root.mainloop()
