简体   繁体   English

为什么我的 Python tkinter 复选框没有出现?

[英]Why doesn't my Python tkinter Checkbox appear?

so I tried putting a checkbox into my tkinter window with this code:所以我尝试使用以下代码在我的 tkinter window 中添加一个复选框:

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?我不知道要为“自我”传递什么...有人可以帮我吗?

IntVar() does not take any parameters which is what caused the problem. IntVar()不采用导致问题的任何参数。 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.也可以更改变量或检查按钮名称,否则程序将抛出另一个错误,说明 checkbutton 属性没有 get() 方法。 I added an "n" to the checkbutton object but you can change this if you wish.我在检查按钮 object 中添加了一个“n”,但如果您愿意,可以更改它。


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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM