网络编程
位置:首页>> 网络编程>> Python编程>> python中Tkinter复选框Checkbutton是否被选中判断

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

作者:Gordennizaicunzai  发布时间:2023-10-20 16:41:19 

标签: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

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

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()

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

  • 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中Tkinter复选框Checkbutton是否被选中判断

来源:https://blog.csdn.net/Gordennizaicunzai/article/details/122724963

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com