python中Tkinter复选框Checkbutton是否被选中判断
作者:Gordennizaicunzai 时间:2023-10-20 16:41:19
Tkinter复选框Checkbutton是否被选中判断
定义一个BooleanVar型数据进行获取复选框状态。
>>> import tkinter as tk
>>>
>>> window = tk.Tk()
>>> var = tk.BooleanVar()
>>> def get_var():
print(var.get())
>>> cb = tk.Checkbutton(window, text="debug", variable=var, command=get_var)
>>> cb.pack()
>>> window.mainloop()
True
False
True
False
True
tkinter-checkbutton详解
介绍checkbutton的使用,由于checkbutton非常简单,所以本文的内容也非常的轻松,让我们开始吧!
checkbutton
:checkbutton也就是我们常说的复选框。text
:设置checkbutton显示的文字bg
:设置背景颜色fg
:设置前景颜色bd
:设置checkbutton的边框宽度relief
:设置显示样式underline
:设置显示的文字是否带下划线state
:checkbutton是否响应用户操作, 值为’normal’,‘active’,‘disabled’
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('渔道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
chkbt = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=10, relief='raised', underline=0, command=test_cb)
chkbt2 = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=5, relief='raised')
chkbt.pack()
chkbt2.pack()
print(chkbt['state'])# 输出 normal
chkbt['state'] = 'disabled' # 将chkbt设置为 不可操作状态,整个checkbutton变成灰色状态
print(chkbt['variable'])# 输出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 输出 checkbutton_yudao
main_win.mainloop()
onvalue
:checkbutton 被选中时的状态值,默认为1offvalue
:checkbutton 未被选中时的状态值,默认为0variable
:checkbutton的全局名,默认系统会自动给分配,也支持自定义。
常见用法是 记录checkbutton的选中状态值,这个属性的命名也很有意思,variable,就传递了一个信息,variable的值是一个变量,所以,常用IntVar作为variable属性的值。
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('渔道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
chkbt = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=10, relief='raised', underline=0, command=test_cb)
chkbt2 = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=5, relief='raised')
chkbt.pack()
chkbt2.pack()
print(chkbt['state'])# 输出 normal
chkbt['state'] = 'disabled' # 将chkbt设置为 不可操作状态,整个checkbutton变成灰色状态
print(chkbt['variable'])# 输出 !checkbutton
chkbt['variable'] = 'checkbutton_yudao'
print(chkbt['variable']) # 输出 checkbutton_yudao
main_win.mainloop()
因为没法截图,所以自行运行后查看效果。
因为是多选框,通过 variable对应的变量来判断对应的checkbutton的选中状态。
例如,这个实例代码中,可以通过val和val2来判断对应的checkbutton是否选中,然后在做对应的处理。
select()
:使checkbutton处于选中状态(on-state)deselect()
:使checkbutton处于选中未状态(off-state)toggle()
:切换checkbutton的选中状态
from tkinter import Tk,Checkbutton
main_win = Tk()
main_win.title('渔道的checkbutton控件')
width = 300
height = 300
main_win.geometry(f'{width}x{height}')
def test_cb():
print(lan_c['state'])
print(lan_c['variable'])
print(lan_c['tristatevalue'])
print(lan_c['onvalue'])
print(lan_c['offvalue'])
lan_python = Checkbutton(main_win, text='python', bg='yellow')
lan_c = Checkbutton(main_win, text='c', bg='blue', command=test_cb, relief='raised', bd=5)
lan_c_plus_plus = Checkbutton(main_win, text='c++', bg='yellow', underline=0)
lan_java = Checkbutton(main_win, text='java', bg='blue')
lan_php = Checkbutton(main_win, text='php', bg='yellow')
lan_html5 = Checkbutton(main_win, text='html5', bg='blue')
lan_js = Checkbutton(main_win, text='javascript', bg='yellow')
# 左对齐
lan_python.pack(anchor='w')
lan_c.pack(anchor='w')
lan_c_plus_plus.pack(anchor='w')
lan_java.pack(anchor='w')
lan_php.pack(anchor='w')
lan_html5.pack(anchor='w')
lan_js.pack(anchor='w')
lan_c_plus_plus.select()# 将lan_c_plus_plus设置为选中状态
lan_c_plus_plus.deselect()# 将lan_c_plus_plus设置为未选中状态
lan_c_plus_plus.toggle()# 切换lan_c_plus_plus的状态
main_win.mainloop()
来源:https://blog.csdn.net/Gordennizaicunzai/article/details/122724963
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python绘制神器五角星+小黄人+樱花
![](https://img.aspxhome.com/file/2023/8/107958_0s.png)
吴恩达机器学习练习:神经网络(反向传播)
![](https://img.aspxhome.com/file/2023/6/80656_0s.png)
你是一个职业的页面重构工作者吗?
Httprunner简介、安装及基本使用教程
![](https://img.aspxhome.com/file/2023/0/110260_0s.png)
用python爬取租房网站信息的代码
python代码实现将列表中重复元素之间的内容全部滤除
![](https://img.aspxhome.com/file/2023/5/63585_0s.png)
joomla组件开发入门教程
使用Go添加HTTPS的实现代码示例
Python中xlsx文件转置操作详解(行转列和列转行)
![](https://img.aspxhome.com/file/2023/0/92310_0s.jpg)
python爬虫scrapy基于CrawlSpider类的全站数据爬取示例解析
![](https://img.aspxhome.com/file/2023/1/79701_0s.jpg)
python判断设备是否联网的方法
微信跳一跳python代码实现
python3+opencv生成不规则黑白mask实例
![](https://img.aspxhome.com/file/2023/5/72995_0s.jpg)
pytest-sugar 执行过程中显示进度条的脚本分享
Django CBV与FBV原理及实例详解
![](https://img.aspxhome.com/file/2023/8/93838_0s.png)
python出现RuntimeError错误问题及解决
![](https://img.aspxhome.com/file/2023/6/102906_0s.png)