1. 程式人生 > 實用技巧 >Python tkinter之CheckButton(多選框)

Python tkinter之CheckButton(多選框)

1、checkbutton的基本屬性

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

import tkinter
from tkinter import *


def event():
    if value.get() == 1:
        print('選中')
    else:
        print('取消選中')


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 = IntVar() value.set(0) # 不設定預設為0 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), # 字型 variable=value, # 通過IntVar設定1為選中,0為未選中 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"

③邊框樣式,元件狀態閱覽