簡體   English   中英

如何更改已禁用的TTK按鈕的前景色?

[英]How to change foreground color of a ttk button that is disabled?

當我禁用按鈕時,顏色會自動變為黑色。 這是代碼:

from tkinter import *
from tkinter import ttk
root=Tk()

style=ttk.Style()
style.configure('TButton', foreground='red')
bu1=ttk.Button(root, text="Hello world")
bu1.grid(row=0, column=0)

bu2=ttk.Button(root, text="Hello world2")
bu2.grid(row=1, column=0)

bu1.state(['disabled'])
bu2.state(['disabled'])

root.mainloop()

有什么幫助嗎?

由於使用的是ttk按鈕,因此可以使用樣式對象的map方法將某些屬性映射到不同的按鈕狀態。

例如,要在按鈕狀態為"disabled"時更改顏色,可以這樣設置顏色:

style.map(
        "TButton",
        foreground=[("disabled", "black")]
)

有關更多信息,請參見50.2。 ttk樣式圖: New Mexico Tech tkinter文檔上的動態外觀更改 ,以及tkdocs.com上的樣式和主題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM