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

限于喜欢 提交于 2021-02-05 11:27:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!