如何更改禁用的 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")]
)

有关详细信息,请参阅 tkdocs.com

上的 50.2. ttk style maps: dynamic appearance changes on the New Mexico Tech tkinter documentation, and also Styles and Themes