您将需要创建自定义样式,然后将该样式应用于小部件。要创建自定义样式,首先获取 的实例ttk.Style
,然后使用该configure
方法从现有样式派生新样式。以下示例创建一个名为“Red.TCheckbutton”的新样式:
style = ttk.Style()
style.configure("Red.TCheckbutton", foreground="red")
接下来,当您想要更改颜色时,只需将此样式与小部件相关联:
my_checkbutton.configure(style="Red.TCheckbutton")
学习如何使用 ttk 样式的最佳资源是tkdocs.com。具体来说,https://www.tkdocs.com/tutorial/styles.html。
这是一个完整的工作示例:
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)
red_button.pack(side="left")
default_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()