问题
When I disable a button the color change automatically to black. This is the code :
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()
Any help?
回答1:
Since you are using a ttk button, you can map certain attributes to different button states with the map
method of the style object.
For example, to change the colors when the button state is "disabled"
, you can set the color like this:
style.map(
"TButton",
foreground=[("disabled", "black")]
)
For more information see 50.2. ttk style maps: dynamic appearance changes on the New Mexico Tech tkinter documentation, and also Styles and Themes on tkdocs.com
来源:https://stackoverflow.com/questions/53104438/how-to-change-foreground-color-of-a-ttk-button-that-is-disabled