tkk checkbutton appears when loaded up with black box in it tkk checkbutton appears when loaded up with black box in it tkinter tkinter

tkk checkbutton appears when loaded up with black box in it


I've had a similar issue on Windows 7.

After loading the app, one of my checkbuttons contained a filled square. But after clicking on it, it became a normal checkbutton:

enter image description here

In my case, it was because I had multiple checkbuttons sharing the same variable... After creating a separate Tk.IntVar() variable for each checkbutton, the problem disappeared.

import Tkinter as Tkimport ttkroot = Tk.Tk()checkVar = Tk.IntVar()x = ttk.Checkbutton(root, variable=checkVar, text="check 1")x.pack()checkVar2 = Tk.IntVar()y = ttk.Checkbutton(root, variable=checkVar2, text="check 2")y.pack()root.mainloop()


I hit this problem when creating a Checkbutton object from within a class. I was declaring a local variable instead of a member variable in the class. The local variable was getting out of scope causing the checkbox value to not be either a 0 or a 1.

Wrong:

    import tkinter as Tk    from tkinter import IntVar    from tkinter.ttk import Frame, Checkbutton    class TestGui(Frame):        def __init__(self, parent):            Frame.__init__(self, parent)            var1 = IntVar()            var1.set(1)            button = Checkbutton(parent,                text="Pick me, pick me!",                variable=var1)            button.grid()    root = Tk.Tk()    app = TestGui(root)    root.mainloop()

Fixed:

import tkinter as Tkfrom tkinter import IntVarfrom tkinter.ttk import Frame, Checkbuttonclass TestGui(Frame):    def __init__(self, parent):        Frame.__init__(self, parent)        self.var1 = IntVar()        self.var1.set(1)        button = Checkbutton(parent,            text="Pick me, pick me!",            variable=self.var1)        # note difference here        button.grid()root = Tk.Tk()app = TestGui(root)root.mainloop()