tkinter 教程 多选按钮——迹忆客-ag捕鱼王app官网
checkbutton
多选按钮控件是一个包含多个可选项的控件。多选按钮可以包含文本或图像。它还可以绑定单击多选按钮时要调用的回调函数。
要在现有父窗口中创建一个 checkbutton 窗口控件,请使用
tk.checkbutton(parent, option, ...)
tkinter checkbutton 基本示例
import tkinter as tk
app = tk.tk()
app.geometry('150x100')
chkvalue = tk.booleanvar()
chkvalue.set(true)
chkexample = tk.checkbutton(app, text='check box', var=chkvalue)
chkexample.grid(column=0, row=0)
app.mainloop()
chkvalue = tk.booleanvar()
每个多选按钮都应与变量相关联。选择/取消选择时,变量的数据类型由多选按钮来确定。
chkvalue.set(true)
它设置了多选按钮的原始值。因为它只是赋予为 boolean 数据类型,所以这里只有两个选项,true 或 false。
chkexample = tk.checkbutton(app, text='check box', var=chkvalue)
现在,这里新建了一个 checkbutton 实例,并且使用我们在上面创建的变量 chkvalue。
chkexample.grid(column=0, row=0)
除了在 tkinter 标签部分中引入的 pack,grid 是另一种类型的 tkinter 布局管理器。
tkinter 有三个布局管理器,
- pack - 打包
- grid - 网格
- place - 放置
我们将在本教程的后半部分介绍这些布局/几何管理器。
tkinter checkbutton 选择/取消选择
用户可以单击 gui 中的多选按钮来选择或取消选择按钮。你也可以使用 select()
和 deselect()
方法选择或取消选中 checkbutton。
chkexample.select()
print "the checkbutton value when selected is {}".format(chkvalue.get())
chkexample.select()
print "the checkbutton value when deselected is {}".format(chkvalue.get())
结果如下
the checkbutton value when selected is true
the checkbutton value when deselected is false
这里,checkbutton 的值是通过 get()
方法获得的。
tkinter checkbutton 多选按钮状态切换
多选按钮的状态可以通过 select()
和 deselect()
修改,也可以使用 toggle() 方法切换。
import tkinter as tk
app = tk.tk()
app.geometry('150x100')
chkvalue = tk.booleanvar()
chkvalue.set(true)
chkexample = tk.checkbutton(app, text='check box', var=chkvalue)
chkexample.grid(column=0, row=0)
print "the checkbutton original value is {}".format(chkvalue.get())
chkexample.toggle()
print "the checkbutton value after toggled is {}".format(chkvalue.get())
chkexample.toggle()
print "the checkbutton value after toggled twice is {}".format(chkvalue.get())
app.mainloop()
输出结果
the checkbutton original value is true
the checkbutton value after toggled is false
the checkbutton value after toggled twice is true
tkinter checkbutton 回调函数绑定
checkbutton 控件用于选择状态,它还可以在选择/取消选择或更直接,切换时将回调函数绑定到事件。每当切换多选按钮状态时,都会触发回调函数。
import tkinter as tk
from _cffi_backend import callback
def callbackfunc():
print "oh. i'm clicked"
app = tk.tk()
app.geometry('150x100')
chkvalue = tk.booleanvar()
chkvalue.set(true)
chkexample = tk.checkbutton(app, text='check box',
var=chkvalue, command=callbackfunc)
chkexample.grid(column=0, row=0)
app.mainloop()
每当你按下按钮时,你都会看到它在 console 中打印出了 oh. i'm clicked。
checkbutton
类中的选项 command 用于在按下按钮时绑定回调函数或方法。
更改 tkinter checkbutton 多选按钮默认值
与未选中的 checkbutton 对应的默认值为 0,并且所选的 checkbutton 的默认值为 1.你还可以将 checkbutton 默认值及其关联的数据类型更改为其他值和/或数据类型。
import tkinter as tk
app = tk.tk()
app.geometry('150x100')
chkvalue = tk.stringvar()
chkexample = tk.checkbutton(app, text='check box', var=chkvalue,
onvalue="rgb", offvalue="ycbcr")
chkexample.grid(column=0, row=0)
chkexample.select()
print "the checkbutton value when selected is {}".format(chkvalue.get())
chkexample.deselect()
print "the checkbutton value when deselected is {}".format(chkvalue.get())
app.mainloop()
the checkbutton value when selected is rgb
the checkbutton value when deselected is ycbcr
chkexample = tk.checkbutton(app, text='check box', var=chkvalue,
onvalue="rgb", offvalue="ycbcr")
onvalue
和 offvalue
是更改所选和未选状态值的选项。它们可以有数据类型,如 int,string,float 或其他。
注意
跟 checkbutton 多选按钮相关联的 tkinter 数据类型应该跟它的 onvalue 和 offvalue 的数据类型相同。否则就会有_tkinter.tclerror
错误。