Check Buttons
checkbutton小部件用于显示切换按钮的用户多项选择。然后,用户可以通过点击相应的按钮每个选项中选择一个或多个选项.每个选项可以表示两种状态:On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用。
语法:
这里是一个简单的语法来创建这个widget:
w = Checkbutton ( master, option, ... )
参数:
-
master: 这表示父窗口.
-
options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.
Option | Description |
---|---|
activebackground | Background color when the checkbutton is under the cursor. |
activeforeground | Foreground color when the checkbutton is under the cursor. |
bg | The normal background color displayed behind the label and indicator. |
bitmap | To display a monochrome image on a button. |
bd | The size of the border around the indicator. Default is 2 pixels. |
command | A procedure to be called every time the user changes the state of this checkbutton. |
cursor | If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the checkbutton. |
disabledforeground | The foreground color used to render the text of a disabled checkbutton. The default is a stippled version of the default foreground color. |
font | The font used for the text. |
fg | The color used to render the text. |
height | The number of lines of text on the checkbutton. Default is 1. |
highlightcolor | The color of the focus highlight when the checkbutton has the focus. |
image | To display a graphic image on the button. |
justify | If the text contains multiple lines, this option controls how the text is justified: CENTER, LEFT, or RIGHT. |
offvalue | Normally, a checkbutton's associated control variable will be set to 0 when it is cleared (off). You can supply an alternate value for the off state by setting offvalue to that value. |
onvalue | Normally, a checkbutton's associated control variable will be set to 1 when it is set (on). You can supply an alternate value for the on state by setting onvalue to that value. |
padx | How much space to leave to the left and right of the checkbutton and text. Default is 1 pixel. |
pady | How much space to leave above and below the checkbutton and text. Default is 1 pixel. |
relief | With the default value, relief=FLAT, the checkbutton does not stand out from its background. You may set this option to any of the other styles |
selectcolor | The color of the checkbutton when it is set. Default is selectcolor="red". |
selectimage | If you set this option to an image, that image will appear in the checkbutton when it is set. |
state | The default is state=NORMAL, but you can use state=DISABLED to gray out the control and make it unresponsive. If the cursor is currently over the checkbutton, the state is ACTIVE. |
text | The label displayed next to the checkbutton. Use newlines ("\n") to display multiple lines of text. |
underline | With the default value of -1, none of the characters of the text label are underlined. Set this option to the index of a character in the text (counting from zero) to underline that character. |
variable | The control variable that tracks the current state of the checkbutton. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above. |
width | checkbutton的默认宽度取决于所显示的图像或文字的大小。您可以设置此选项的字符数和checkbutton的,总是有许多字符的空间. |
wraplength | Normally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number. |
方法:
以下是这个小工具的常用方法:
Medthod | Description |
---|---|
deselect() | Clears (turns off) the checkbutton. |
flash() | Flashes the checkbutton a few times between its active and normal colors, but leaves it the way it started. |
invoke() | You can call this method to get the same actions that would occur if the user clicked on the checkbutton to change its state. |
select() | Sets (turns on) the checkbutton. |
toggle() | Clears the checkbutton if set, sets it if cleared. |
此部分来源:http://www.cnblogs.com/tkinter/p/5628887.html
通过回调函数改变Checkbutton的显示文本text的值
from Tkinter import *
def callCheckbutton():
#改变v的值,即改变Checkbutton的显示值
v.set('check CheckButton')
root = Tk()
v = StringVar()
v.set('check python')
#绑定v到Checkbutton的属性textvariable
Checkbutton(root,textvariable = v,command = callCheckbutton).pack()
root.mainloop()
textvariable使用方法与Button的用法完全相同,使用此例是为了区别Checkbutton的另外的一个属性variable,此属性与textvariable不同,它是与这个控件本身绑定,Checkbutton自己有值:On和Off值,缺省状态On为1,Off为0
from Tkinter import *
root=Tk()
v=IntVar()
def callCB():
root1=Tk()
if v.get()==1:
Label(root1,text='你的选择是python'+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
checkbutton=Checkbutton(root,text='python',anchor='w',command=callCB,variable=v)
checkbutton.grid(row=0,column=0)
root.mainloop()
Checkbutton的值不仅仅是1或0,可以是其他类型的数值,可以通过onvalue和offvalue属性设置Checkbutton的状态值,如下代码将On设置为'python',Off值设置为'Tkinter',程序的打印值将不再是0或1,而是'Tkinter’或‘python’
from Tkinter import *
root=Tk()
v=StringVar()
lis=['python','C++','C','Java']
def callCB():
for m in lis:
if v.get()==m:
root1 = Tk()
Label(root1,text='你的选择是'+v.get()+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
Label(root,text='选择你喜欢的编程语言').pack(anchor=W)
checkbutton=Checkbutton(root,text=lis[0],onvalue=lis[0],command=callCB,variable=v)
checkbutton.pack(anchor=W)
checkbutton1=Checkbutton(root,text=lis[1],onvalue=lis[1],command=callCB,variable=v)
checkbutton1.pack(anchor=W)
checkbutton2=Checkbutton(root,text=lis[2],onvalue=lis[2],command=callCB,variable=v)
checkbutton2.pack(anchor=W)
checkbutton3=Checkbutton(root,text=lis[3],onvalue=lis[3],command=callCB,variable=v)
checkbutton3.pack(anchor=W)
root.mainloop()
选择后显示:
问题:运行程序后默认都选。
Radio Buttons
单选按钮是一种可在多个预先定义的选项中选择出一项的 Tkinter 控件. 单选按钮可显示文字或图片. 显示文字时只能使用预设字体. 该控件可以绑定一个 Python 函数或方法, 当单选按钮被选择时, 该函数或方法将被调用.
单选按钮 (Radio Button) 这个名字来源于收音机 (Radio) 上的调频按钮, 这些按钮用来选择特定波段或预设电台. 如果一个按钮被按下, 其他同类的按钮就会弹起, 即同时只有一个按钮可被按下.
一组单选按钮控件和同一个变量关联. 点击其中一个单选按钮将把这个变量设为某个预定义的值.
修改上面的例子:
from Tkinter import *
root=Tk()
v=IntVar()
lis=['python','C++','C','Java']
def callCB():
for i in range(4):
if v.get()==i:
root1 = Tk()
Label(root1,text='你的选择是'+lis[i]+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
Label(root,text='选择一门你喜欢的编程语言').pack(anchor=W)
radiobutton=Radiobutton(root,text=lis[0],value=0,command=callCB,variable=v)
radiobutton.pack(anchor=W)
radiobutton1=Radiobutton(root,text=lis[1],value=1,command=callCB,variable=v)
radiobutton1.pack(anchor=W)
radiobutton2=Radiobutton(root,text=lis[2],value=2,command=callCB,variable=v)
radiobutton2.pack(anchor=W)
radiobutton3=Radiobutton(root,text=lis[3],value=3,command=callCB,variable=v)
radiobutton3.pack(anchor=W)
root.mainloop()
上面代码冗长,简化如下:
from Tkinter import *
root=Tk()
v=IntVar()
#列表中存储的是元素是元组
language=[('python',0),('C++',1),('C',2),('Java',3)]
def callRB():
for i in range(4):
if v.get()==i:
root1 = Tk()
Label(root1,text='你的选择是'+language[i][0]+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
Label(root,text='选择一门你喜欢的编程语言').pack(anchor=W)
#
for lan,num in language:
Radiobutton(root, text=lan, value=num, command=callRB, variable=v).pack(anchor=W)
root.mainloop()
RadioButton默认的是空心圆形作为选项条目
可以用一个文本框来展示选项. 将 indicatoron 选项设置为 0 即可达到该目的: 这表示单选按钮不会有额外的单选指示器.
from Tkinter import *
root=Tk()
v=IntVar()
#列表中存储的是元素是元组
language=[('python',0),('C++',1),('C',2),('Java',3)]
def callRB():
for i in range(4):
if v.get()==i:
root1 = Tk()
Label(root1,text='你的选择是'+language[i][0]+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
Label(root,text='选择一门你喜欢的编程语言').pack(anchor=W)
#
for lan,num in language:
Radiobutton(root, text=lan, value=num, command=callRB, variable=v,indicatoron=0,width=10,anchor='c').pack()
root.mainloop()
创建不同组:
from Tkinter import *
root=Tk()
v1=v=IntVar()
#列表中存储的是元素是元组
language=[('python',0),('C++',1),('C',2),('Java',3)]
Animal=[('狗',0),('猫',1),('鸟',2)]
def callRB():
for i in range(4):
if v.get()==i:
root1 = Tk()
Label(root1,text='你的选择是'+language[i][0]+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
def callRB1():
for i in range(4):
if v1.get()==i:
root1 = Tk()
Label(root1,text='你的选择是'+Animal[i][0]+'!',fg='red',width=20,height=6).pack()
Button(root1,text='确定',width=3,height=1,command=root1.destroy).pack(side='bottom')
Label(root,text='选择一门你喜欢的编程语言').pack(anchor=W)
for lan,num in language:
Radiobutton(root, text=lan, value=num, command=callRB, variable=v,indicatoron=0,width=10,anchor='c').pack()
Label(root,text='选择喜欢的动物',anchor='c').pack()
for An,num in Animal:
Radiobutton(root, text=An, value=num, command=callRB1, variable=v1, indicatoron=0, width=10, anchor='c').pack()
root.mainloop()