如何使用python中的按钮取消选中复选框?

时间:2016-05-11 19:15:35

标签: python-2.7 tkinter

我需要一个清除所选复选框的按钮。请有人指导我。提前谢谢。

import Tkinter
from Tkinter import*

top = Tkinter.Tk()
CheckVar1=IntVar()
CheckVar2=IntVar()

C1=Checkbutton(top, text = "Music", variable = CheckVar1,
                 onvalue = 1, offvalue = 0, height=5,
                 width = 20,activebackground="red",bg="green")
C2=Checkbutton(top, text = "Video", variable = CheckVar2,
                 onvalue = 1, offvalue = 0, height=5,
                 width = 20)

C1.pack()
C2.pack()

B = Tkinter.Button(top, text ="Hello",activebackground="red",
    ,bd=3,bg="green",width=5) #Button

B.pack()
top.mainloop()

1 个答案:

答案 0 :(得分:8)

创建一个将CheckVar1和CheckVar2的值设置为0的函数。

def clear():
    CheckVar1.set(0)
    CheckVar2.set(0)

然后只需将其与按钮链接即可。

B = Tkinter.Button(top, text ="Hello",activebackground="red",
bd=3,bg="green",width=5, command = clear) #Button
顺便说一下,你这里有一个额外的逗号。