0

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
slv026
  • 1
  • 1
  • 1
    You could try changing the `highlightthickness` parameter, so using the line `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, highlightthickness=0)` I cannot be sure as I did not clearly understand which visual behavior you want to remove, consider adding relevant screenshots – Caridorc Apr 08 '23 at 14:06
  • I don't see nothing wrong. It worked for me. – toyota Supra Apr 08 '23 at 14:15
  • Have you tried using the `selectcolor` attribute? – Bryan Oakley Apr 08 '23 at 22:23
  • @Bryan - selectcolor attribute worked perfectly - thanks – slv026 Apr 09 '23 at 01:59
  • Why not answer your own question? – relent95 Apr 11 '23 at 01:07

0 Answers0