Python tkinter之CheckButton(多选框)

1、checkbutton的基本属性

复制代码

# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *


def event():
print('当前的值:{}'.format(value.get()))


if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南风丶轻语') # 标题
screenwidth = win.winfo_screenwidth() # 屏幕宽度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置

value = StringVar()
checkbutton = Checkbutton(
master=win, # 父容器
text='标签', # 文本
bg='yellow', # 背景颜色
fg='red', # 文本颜色
activebackground='pink', # 状态为active时的背景颜色
activeforeground='blue', # 状态为active的文字颜色
relief='flat', # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。
bd=3, # 边框的大小
height=1, # 高度
width=10, # 宽度
padx=1, # 内间距,字体与边框的X距离
pady=1, # 内间距,字体与边框的Y距离
state='normal', # 设置状态 normal、active、 disabled
cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus...
font=('黑体', 20), # 字体
onvalue='哈哈', # 选中传递的值,默认是1
offvalue='嘿嘿', # 取消选中后传递的值,默认是0
variable=value, # 通过onvalue和offvalue传递
command=event, # 点击时的事件
)
checkbutton.pack()

Label(win, textvariable=value, relief='g', width=10).pack(padx=20) # 用于显示value的值

win.mainloop()
 
复制代码

备注:

①支持的字体(通过tkinter.font.families获取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/

②鼠标样式选项

"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus","shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"

③边框样式,组件状态阅览

 

posted @   南风丶轻语  阅读(2767)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示