问题描述
我已经进行了全面搜索,但是还没有找到一个简单的示例来说明如何更改ttk小部件样式的次要元素并动态地应用它(在创建小部件之后)。
I've searched all over, but have yet to find a simple example showing how to change a minor element of a ttk widget style and applying it dynamically (after widget creation).
我有一些代表某些配置项目的ttk复选框,还有一个用于更新远程系统的ttk按钮。选中按钮被初始化为远程系统的状态。
I have some ttk checkbuttons representing some configuration items, and a ttk button used to update a remote system. The checkbuttons are initialized to the state of the remote system.
如果用户修改了任何选中按钮,我希望选中按钮文本和更新按钮文本都变为红色,以提醒用户复选按钮状态不再与远程状态匹配,应按下更新按钮以将修改后的配置发送到远程系统。
If the user modifies any of the checkbuttons, I want both the checkbutton text and the update button text to become red, to remind the user that the checkbutton state no longer matches the remote state, and that the update button should be pressed to send the modified configuration to the remote system.
何时
我知道这是可能的(并且可能很容易),但是怎么办呢?
I know this is possible (and is probably easy), but how?
编辑:修改背景颜色也可以。
Modifying the background color would also be OK.
推荐答案
您将需要创建自定义样式,然后将该样式应用于窗口小部件。要创建自定义样式,请首先获取 ttk.Style
的实例,然后使用 configure
方法派生一个现有的新样式。下面的示例创建一个名为 Red.TCheckbutton的新样式:
You will need to create a custom style, and then apply that style to the widget. To create a custom style, first get an instance of ttk.Style
, and then use the configure
method to derive a new style from an existing one. The following example creates a new style named "Red.TCheckbutton":
style = ttk.Style()
style.configure("Red.TCheckbutton", foreground="red")
接下来,您只需将其关联当您希望更改颜色时,请使用小部件样式:
Next, you simply associate this style with the widget when you want the color to change:
my_checkbutton.configure(style="Red.TCheckbutton")
学习如何使用ttk样式的最佳资源是。具体来说,。
The best resource for learning how to work with the ttk styles is tkdocs.com. Specifically, http://www.tkdocs.com/tutorial/styles.html.
以下是一个完整的示例:
Here's a complete working example:
import ttk
import Tkinter as tk
class ExampleApp(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.var1 = tk.StringVar()
self.var2 = tk.StringVar()
f1 = ttk.Frame(self)
red_button = ttk.Button(f1, text="Red", command=self.make_red)
default_button = ttk.Button(f1, text="Default", command=self.make_default)
default_button.pack(side="left")
red_button.pack(side="left")
f2 = ttk.Frame(self)
self.cb_one = ttk.Checkbutton(f2, text="Option 1", variable=self.var1,
onvalue=1, offvalue=0)
self.cb_two = ttk.Checkbutton(f2, text="Option 2", variable=self.var2,
onvalue=1, offvalue=0)
self.cb_one.pack(side="left")
self.cb_two.pack(side="left")
f1.pack(side="top", fill="x")
f2.pack(side="top", fill="x")
style = ttk.Style()
style.configure("Red.TCheckbutton", foreground="red")
def make_red(self):
self.cb_one.configure(style="Red.TCheckbutton")
self.cb_two.configure(style="Red.TCheckbutton")
def make_default(self):
self.cb_one.configure(style="TCheckbutton")
self.cb_two.configure(style="TCheckbutton")
if __name__ == "__main__":
root = tk.Tk()
ExampleApp(root).pack(fill="both", expand=True)
root.mainloop()
这篇关于更改TTK小部件文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!