Collin Lehmann
Collin Lehmann

Reputation: 69

Why doesn't my Python tkinter Checkbox appear?

so I tried putting a checkbox into my tkinter window with this code:

def a():
    if checkbutton.get():
        print("Checkbox is chosen")
    else:
        print("Checkbox ist not chosen")


checkbutton = IntVar(Checkbutton)
checkbutton =
Checkbutton(self=checkbutton,master=root,text="Pyhon3",variable=checkbutton,command=a)
checkbutton.pack()

and That generated this error:

File "C:\Users\ulric\Desktop\Tkinter.py", line 63, in <module>
    checkbutton = IntVar(Checkbutton)
TypeError: _root() missing 1 required positional argument: 'self'

I don't know what to pass in for "self"... Can someone help me please?

Upvotes: 0

Views: 91

Answers (1)

Sister Coder
Sister Coder

Reputation: 324

IntVar() does not take a class as a parameter which is what caused the problem. Also either change the variable or check button name or the program will throw another error saying the checkbutton attribute does not have a get() method. I added an "n" to the checkbutton object but you can change this if you wish.


def a():
    if checkbutton.get():
        print("Checkbox is chosen")
    else:
        print("Checkbox ist not chosen")


checkbutton = IntVar()
checkbuttonn = Checkbutton(root,text="Pyhon3",variable=checkbutton,command=a)
checkbuttonn.pack()

root.mainloop()

Upvotes: 1

Related Questions