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,IntVar

main_win = Tk()
main_win.title('渔道的checkbutton控件')
width = 300 
height = 300 
main_win.geometry(f'{width}x{height}')

def test_cb():
    print(val.get())  # 当选中chkbt时,打印输出1;当取消选中时,打印输出0
    print(val2.get())	# 当选中chkbt2时,打印输出1;当取消选中时,打印输出0
    
val = IntVar()
val2 = IntVar()
chkbt = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=10, relief='raised', underline=0, command=test_cb, variable=val)
chkbt2 = Checkbutton(main_win, text='python', bg='yellow', fg='red', bd=5, relief='raised', command=test_cb, variable=val2)

chkbt.pack()
chkbt2.pack()

因为没法截图,所以自行运行后查看效果。

因为是多选框,通过 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()

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sif_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值