1. 程式人生 > >python中tkinter的應用:更改複選框Checkbutton背景顏色

python中tkinter的應用:更改複選框Checkbutton背景顏色

由於button不支援觸發後更改顏色,於是選用複選框來充當按鈕控制元件

    # -*- coding: utf-8 -*-
    from tkinter import *
    root = Tk()
    #觸發前為粉色,觸發之後為春綠色
    Checkbutton(root, text="複選按鈕1", width=10, bg="Pink", indicatoron=False,
                selectcolor="SpringGreen").pack(side=tk.TOP)
    #觸發前為紅色,觸發之後為春綠色
    Checkbutton(root, text="複選按鈕2", width=10, bg="red", indicatoron=False,
                selectcolor="SpringGreen").pack(side=tk.TOP)
    #觸發前為黃色,觸發之後為藍色
    Checkbutton(root, text="複選按鈕3", width=10, bg="yellow", indicatoron=True,
                selectcolor="blue").pack(side=tk.TOP)
    
    root.mainloop()

下圖分別表示觸發前和觸發後的裝態

引數 indicatoron=Flase 表示取消複選框前面的選擇小方框,如不取消,變色效果將會消失