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


猜你喜欢
- PyCharm 是我用过的python编辑器中,比较顺手的一个。而且可以跨平台,在macos和windows下面都可以用,这点比较好。是py
- 本文实例分析了JS获取年月日时分秒的方法。分享给大家供大家参考,具体如下:var d = new Date();var time = d.g
- 一.axisaxis就是指定轴。三维数组可看作元素是二维数组的一维数组,二维数组可看作元素是一维数组的一维数组 。(这么理解就舒服了!)例:
- 鉴于最近一段时间一直在折腾的CNN网络效果不太理想,主要目标是为了检测出图像中的一些关键点,可以参考人脸的关键点检测算法。但是由于从数据集的
- 前两天在帮朋友整理他的主页空间时候,发现的一点关于MySQL可能大家都会忽略的问题:我们知道,在安装完MySQL后,它会自动创建一个root
- 本文介绍了python画图时设置分辨率和画布大小的实现,主要使用plt.figure(),下面就一起来了解一下plt.figure()示例:
- 一个简单的tokenizer分词(tokenization)任务是Python字符串处理中最为常见任务了。我们这里讲解用正则表达式构建简单的
- 通常我们会用wc -l来统计文件行数,不过用Python统计也很简单。要快速统计一个文本文件中的行数,其实就是要统计这个文本文件中换行符的个
- 表格如下:在Unity读取并调用时的代码:而如果想要查看该数据库中的另一个表,不是直接使用Table[1],而是需要更改SELECT * f
- 引言你知道什么是依赖注入吗?依赖注入(DI)的概念虽然听起来很深奥,但是如果你用过一些新兴的php框架的话,对于DI一定不陌生,因为它们多多
- 前几天有位微信读者问我一个爬虫的问题,就是在爬去百度贴吧首页的热门动态下面的图片的时候,爬取的图片总是爬取不完整,比首页看到的少。原因他也大
- 一大早就被电话吵醒了,云某项目数据库全挂了,启动不了(睡得太死,没听到报警短信),吓得不轻啊!电话中说所有mysql数据库主库都启动不了,但
- 1. list查询个数:调用list.count(obj)函数,返回obj在list中的个数。输入:list_a = [2 for x in
- 目录项目引入flask-sqlalchemyORM简介及模型定义表关系类型及编码实现一对多关系(多对一关系)一对一关系多对多关系数据库基本操
- 首先澄清一个应用场景问题。研究(1)中指出,对于结构复杂的网站,不少设计师们喜欢采用960固定宽度布局。但要注意的是,960并不是万能钥匙,
- 最近在使用python做接口测试,发现python中http请求方法有许多种,今天抽点时间把相关内容整理,分享给大家,具体内容如下所示:一、
- 1.下载安装Golanghttps://golang.google.cn/dl/一路下一步即可2.下载安装Vscodehttps://vis
- NetworkX是一个非常强大的网络科学工具,它封装了图的数据结构和许多经典图算法,也内置了许多可视化函数可供调用。1. 随机图生成最经典的
- 本文实例讲述了php+js实现的无刷新下载文件功能。分享给大家供大家参考,具体如下:服务器端页面步骤就是,设置头文件参数,然后读入并输出文件
- 移动端适配满足多个查询时的优先级: 请注意,可以同时满足多个查询,并且它们都将由mergeOption合并,mergeOption稍后由me