2016-05-11 149 views
-1

我需要一個按鈕來清除選中的複選框。請任何人指導我。提前致謝。如何在python中使用按鈕取消選擇複選框?

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

您是否嘗試過簡單地將關聯的變量設置爲零? –

+0

是的,但它沒有工作.. –

+0

它適用於我的代碼張貼在您的問題。我添加了一個調用「CheckVar1.set0」和「CheckVar2.set(0)」的函數,它工作得很好。當你說它不適合你時,請準確顯示你如何做,以及你得到的任何錯誤。 –

回答

6

創建將CheckVar1和CheckVar1的值設置爲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 

順便說一句,你在這裏有一個額外的逗號。

相關問題