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
猜你喜欢
- Profile 和 cProfile在 Python 标准库里面有两个模块可以用来做性能测试。1. 一个是 Profile,它是一个纯 Py
- 默认值可以很方便众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属
- 如何编写CSS代码才能更有效率?这是许多网页制作者与开发者都关心的问题。大概没有什么魔法,可以保证一下就把你的样式表缩小到百分之多少,但合理
- 使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码。随机密码也就是一
- 本文分析了PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的区别。分享给大家供大家参考,具
- 动态联接库(DLL)是加快应用程序关键部分的执行速度的重要方法,但有一点恐怕大部分人都不知道,那就是在ASP文件也能通过调用DLL来加快服务
- 第一:编写限制搜索范围的查询语句。众所周知,在数据库查询的时候返回记录的多少直接关系到查询的效率。所以,在客户端通过一定的条件语句,限制搜索
- 获取一组radio被选中项的值var item = $(’input[@name=items][@checke
- 以下的文章主要是对MySQL性能影响关系紧密的五大配置参数的介绍,我前几天在相关网站看见对MySQL性能影响关系紧密的五大配置参数的资料,觉
- 1.实现的思路(1)首先使用一个处理画框的程序,将图片中的有车和无车的停车位给画出来,并且保存坐标(如果画错了,将鼠标移至要删除的框中,右击
- MySQL采用了基于开销的优化器,以确定处理查询的最解方式。在很多情况下,MySQL能够计算最佳的可能查询计划,但在某些情况下,MySQL没
- 来源:奥索网 桌面中心(二)数据库写入作者:elong 一、以下是数据库的写入的html程序,你可以加入密码功能。把密码做成变量发入下面那个
- 前言HI,好久不见,今天是关闭朋友圈的第60天,我是野蛮成长的AC-Asteroid。人生苦短,我用Python,通过短短两周时间自学,从基
- memcache 的工作就是在专门的机器的内存里维护一张巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率,减
- 1.流程控制语 break用于结束整个循环结构,直接退出整个循环例:用两种循环模拟密码输入输入正确就退出循环,输入错误可以重新输入,有三次输
- 在MySQL中,对于索引的使用并是一直都采用正确的决定。简单表的示例:CREATE TABLE `r2` (ID` int(11) DEFA
- 上一篇中,我们已经打开了Django自带的用户认证模块,并配置了数据库连接,创建了相应的表,本篇我们将在Django自带的用户认证的基础上,
- 参考服务器安装的是Centos 系统。uwsgi是使用pip安装的。nginx是使用yum install nginx安装。python 2
- select for update 这个是行级锁 当 commit或者rollback时,锁释放 记得打开事务,比如jdbc里面 setAu
- 效果如下所示:# -*- coding: utf-8 -*-import turtle# 绘制太极图函数def draw_TJT(R):&n