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:
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 theselectcolorand the check mark will use thefgcolor)
Neither of these is a perfect solution, unfortunately, but they will work.