tkinter的IntVar()和.get()'ing i

2024-06-14 09:43:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里需要一点帮助,我觉得我已经无休止地寻找,无法解决我的问题。

案例:

我有一个复选按钮,它的on值为1,off值为0,现在我想做一个动作,如果它是on,另一个是off。

我的密码是:

#==Checkbox==#

check = IntVar()
checkbox = Checkbutton(labelframe, text="Tillad mere end én linje", variable = check, onvalue=1, offvalue=0)
checkbox.pack(side = RIGHT)

...

def go():

        check.get()

        print(check)

        if(check == 0):

                print("off")

                w.delete(ALL)
                tegnefladen()
                update()

        else:

                print("on")
                update()

Tags: text密码oncheckupdate按钮案例print
1条回答
网友
1楼 · 发布于 2024-06-14 09:43:25

你实际上并没有设置这个值。check是一个对象,它永远不会与0相同。基本上,您需要比较check.get()。试试这个:

def go():
        print(check.get())
        if(check.get() == 0):

                print("off")

                w.delete(ALL)
                tegnefladen()
                update()

        else:

                print("on")
                update()

相关问题 更多 >