Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkk checkbutton appears when loaded up with black box in it

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

like image 740
Ab Bennett Avatar asked Mar 26 '17 14:03

Ab Bennett


1 Answers

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()
like image 186
zambamingi Avatar answered Oct 24 '22 15:10

zambamingi