Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter's checkbutton makes checkmark invisible after changing background color

I am using tkinter to create a simple GUI with python. I wanted to change the background and foreground color of a Checkbutton and while that works, it makes the checkmark itself invisible. It still works, tested with a simple function, but I seem to miss something. Here is the code:

import tkinter as tk

def check1Clicked():
    if var1.get():
        print("clicked")
    else:
        print("not clicked")


root = tk.Tk()
root.geometry("100x100")

var1 = tk.IntVar()
c1   = tk.Checkbutton(root, text="bla", variable=var1,
         onvalue=1, offvalue=0,
         bg="red",fg="white",
         command=check1Clicked
         )
# use "place" to easily center widget
c1.place(relx=.5, rely=.5, anchor="c")

root.mainloop()

Any suggestions what I am missing would be greatly appreciated!

The versions I am using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

python:  3.8.10
tkinter: 8.6

>Solution :

The problem appears to be caused by fg='white', because the check mark matches the foreground color. Ergo, when the box is checked, the checkmark is white on a white background.

To alleviate this, you can either

  • Change to fg=black (or omit it entirely since black is the default), but this has the effect of making your text black as well

or

  • Change the background color of the checkbox itself with selectcolor=black, so the checkmark will appear as white on a black background (you could really use any color here, just know that the box will use the selectcolor and the check mark will use the fg color)

Neither of these is a perfect solution, unfortunately, but they will work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading