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"
③邊框樣式,組件狀態閱覽