Image On Off switch

Tkinter Buttons Image

Button with On image
Button with Off image

Here are two images used. ( Right click and save images in PNG format )

On off switch image OFF position On off switch image ON position


Tkinter button with ON OFF images switch to configure window background colour for night mode


We can use this as Night Mode switch and change the window background colour. Here the variables are set to two different colours, this can be changed to match your requirements.
import tkinter as tk
my_w = tk.Tk() 
my_w.geometry("400x300")  # Size of the window
colour_night='lightgreen' # Change the night colour 
colour_day='blue'         # change the day colour 
my_w.configure(background=colour_day) # default background of window
my_img_on = tk.PhotoImage(file = "D:\\testing\\on-off-on.png")
my_img_off = tk.PhotoImage(file = "D:\\testing\\on-off-off.png") 
def my_upd():
    if(b1['bg']==colour_day):
        b1.config(image=my_img_on,bg=colour_night,activebackground=colour_night)
        my_w.configure(background=colour_night)
    else:
        b1.config(image=my_img_off,bg=colour_day,activebackground=colour_day)
        my_w.configure(background=colour_day)
    
b1=tk.Button(my_w,image=my_img_off,
    relief='flat',bg=colour_day,command=lambda:my_upd())
b1.grid(row=1,column=1,padx=20,pady=10)
my_w.mainloop()  # Keep the window open

Resizing image using PIL Upload and display image file Colorchooser
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer