Python - Checkbutton background colour changes between on and off

31 Views Asked by At

When using images to replace the standard checkbutton(cb) box, the cb background is set to 'lightpurple' and is the colour when the cb is set to 'on'. When the cb is then selected, cb changes to 'off' but the cb colour becomes 'white (clear)' rather than remaining 'lightpurple'. Selecting the cb again returns to the 'on' condition and the background to 'lightpurple'. Can someone suggest how to stop the cb from changing colour when selecting between on/off?

from tkinter import *
import tkinter.font as font      # This lets us use different fonts.

def showCheckbuttons():
    order_label = Label(lights_frame, text="Select Light(s)",
                        font=font_large, bd=10, padx=80, bg=lightpurple)
    order_label.grid(row=1, column=0, columnspan=2)

    light_list = ["Front  ", "Drive  ", "Back   ", "Side   "]
    light_dict = {}
    
    r=2 #start row
    c=0 #start column
    # Populate the dictionary with checkbutton widgets
    for  i, light_item in  enumerate(light_list):

        # Set the text for each checkbutton
        light_dict[light_item] = Checkbutton(lights_frame, text=light_item, image=ON_img,
                                            selectimage=OFF_img, compound='right',borderwidth=0,
                                            font=font_large, bg=lightpurple,indicatoron=False)

        # Create a new instance of IntVar() for each checkbutton
        light_dict[light_item].var = IntVar()

        # Set the variable parameter of the checkbutton
        light_dict[light_item]['variable'] = light_dict[light_item].var

        # Arrange the checkbutton in the window
        light_dict[light_item].grid(row=r, column=c)
        r=r+1


root=Tk()
lightpurple= '#d0bafe' 
root.configure(bg=lightpurple)
width, height = 500, 420

#Show frame 
lights_frame = Frame(root, bg=lightpurple, width=500, height=420)

# get images for checkbutton on/off
ON_img = PhotoImage(file='ON_small2.png')
OFF_img = PhotoImage(file='OFF_small2.png')

# Create the fonts
font_large = font.Font(family='Courier', size=24, weight='bold')

showCheckbuttons()
lights_frame.grid()

root.mainloop()
0

There are 0 best solutions below