6

I create a check button / box, with the following call

x=ttk.Checkbutton(tab1,state='disabled',command = lambda j=i,x=k: fCheckButton(j,x))
x.state(['selected'])

The box appears fine and is selected, but it appears on load up, with a black box in it, which seems to have nothing to do with the state of it.

I have looked for reasons why, but can't actually find anyone with the same problem.

thanks

martineau
  • 119,623
  • 25
  • 170
  • 301
Ab Bennett
  • 1,391
  • 17
  • 24
  • On some OS's / versions of python / versions of Tcl (I'm not sure which) the "selected" state is a dark square in the middle, and on others it's a checkmark. – Novel Mar 26 '17 at 16:59
  • seems odd to me, is there anyway to stop this. If i know a tick box should be ticked on load, i want to see a tick, not a square that hides this. – Ab Bennett Mar 27 '17 at 11:28

3 Answers3

6

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 Tk
from tkinter import IntVar
from tkinter.ttk import Frame, Checkbutton
class 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()
user3486184
  • 2,147
  • 3
  • 26
  • 28
zambamingi
  • 61
  • 1
  • 2
4

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 Tk
import ttk

root = 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()
Josselin
  • 2,593
  • 2
  • 22
  • 35
0

Apparently, when created, checkbuttons default to an 'alternate' state which is reflected with the 'filled square'. In my case, to get rid of the filled square I had to manually set the state to 'not alternate':

mybutton = ttk.Checkbutton(root, text=foo, variable=myvar, onvalue=1,
                           offvalue=0)
mybutton.state(['!alternate'])
mybutton.pack()
Dgomn D
  • 71
  • 4