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