標籤:python
1.一個簡單的Checkbutton例子
from tkinter import *root = Tk()Checkbutton(root,text = 'python').pack()root.mainloop()
2.設定Checkbutton的回呼函數
from tkinter import *def callCheckbutton(): print ('you check this button')root = Tk()Checkbutton(root,text = 'check python',command = callCheckbutton).pack() #不管Checkbutton的狀態如何,此回呼函數都會被調用root.mainloop()
3.擷取Checkbuton的On與Off的值。預設預設值分別為1和0
from tkinter import *root = Tk()#將一整數與Checkbutton的值綁定,每次點擊Checkbutton,將列印出當前的值v = IntVar()def callCheckbutton(): print (v.get())Checkbutton(root, variable = v, text = 'checkbutton value', command = callCheckbutton).pack()root.mainloop()
from tkinter import *root = Tk()#將一字串與Checkbutton的值綁定,每次點擊Checkbutton,將列印出當前的值v = StringVar()def callCheckbutton(): print v.get()Checkbutton(root, variable = v, text = 'checkbutton value', onvalue = 'python', #設定On的值 offvalue = 'tkinter', #設定Off的值 command = callCheckbutton).pack()root.mainloop()
Python GUI 05----Checkbutton