python中Tkinter复选框Checkbutton是否被选中判断

 更新时间:2023年01月28日 09:31:36   作者:Gordennizaicunzai  
这篇文章主要介绍了python中Tkinter复选框Checkbutton是否被选中判断方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

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 被选中时的状态值,默认为1
  • offvalue:checkbutton 未被选中时的状态值,默认为0
  • variable: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()

在这里插入图片描述

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python数学建模PuLP库线性规划实际案例编程详解

    Python数学建模PuLP库线性规划实际案例编程详解

    本节以一个实际数学建模案例,来为大家讲解PuLP求解线性规划问题的建模与编程。来巩固加深大家对Python数学建模PuLP库线性规划的运用理解
    2021-10-10
  • QML使用Python的函数过程解析

    QML使用Python的函数过程解析

    这篇文章主要介绍了QML使用Python的函数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • PyTorch中torch.nn模块的实现

    PyTorch中torch.nn模块的实现

    torch.nn是PyTorch中用于构建神经网络的核心模块,包括多种组件,每个组件都有其特定的原理和使用场景,本文就来详细的介绍一下如何使用,感兴趣的可以了解一下
    2024-09-09
  • python中opencv Canny边缘检测

    python中opencv Canny边缘检测

    这篇文章主要介绍了python中opencv Canny边缘检测,Canny边缘检测是一种使用多级边缘检测算法检测边缘的方法。OpenCV提供了函数cv2.Canny()实现Canny边缘检测。更多相关内容需要的小伙伴可以参考下面文章内容
    2022-06-06
  • Python 过滤字符串的技巧,map与itertools.imap

    Python 过滤字符串的技巧,map与itertools.imap

    Python中的map函数非常有用,在字符转换和字符遍历两节都出现过,现在,它又出现了,会给我们带来什么样的惊喜呢?是不是要告诉我们,map是非常棒的,以后要多找它玩呢?
    2008-09-09
  • python教程之进程和线程

    python教程之进程和线程

    这篇文章主要为大家介绍了python进程和线程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • python通过第三方库操作PDF文件的几种常见方法

    python通过第三方库操作PDF文件的几种常见方法

    Python是一种高级编程语言,主要用于数据分析、机器学习、图像处理等领域,在PDF文件处理方面,Python有许多强大的库和工具,这篇文章主要给大家介绍了关于python通过第三方库操作PDF文件的几种常见方法,需要的朋友可以参考下
    2024-02-02
  • Python下简易的单例模式详解

    Python下简易的单例模式详解

    这篇文章主要介绍了Python下简易的单例模式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Python OpenCV去除字母后面的杂线操作

    Python OpenCV去除字母后面的杂线操作

    这篇文章主要介绍了Python OpenCV去除字母后面的杂线操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Python 异之如何同时运行多个协程详解

    Python 异之如何同时运行多个协程详解

    这篇文章主要为大家介绍了Python 异之如何同时运行多个协程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03

最新评论