简体   繁体   English

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

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

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? 有什么帮助吗?

Since you are using a ttk button, you can map certain attributes to different button states with the map method of the style object. 由于使用的是ttk按钮,因此可以使用样式对象的map方法将某些属性映射到不同的按钮状态。

For example, to change the colors when the button state is "disabled" , you can set the color like this: 例如,要在按钮状态为"disabled"时更改颜色,可以这样设置颜色:

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

For more information see 50.2. 有关更多信息,请参见50.2。 ttk style maps: dynamic appearance changes on the New Mexico Tech tkinter documentation, and also Styles and Themes on tkdocs.com ttk样式图: New Mexico Tech tkinter文档上的动态外观更改 ,以及tkdocs.com上的样式和主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM