<p>尝试对一些GUI元素使用ttk。ttk允许您为小部件创建样式,并一次将样式更新为所有小部件(至少对于具有相同样式的小部件)。您可能需要混合使用ttk和tkinter,但从长远来看,这应该会使事情变得简单一些。我举了一个例子:</p>
<pre><code>import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# Creating a style for the buttons
color_style_button = ttk.Style()
color_style_button.configure("color.TButton", foreground="red")
def change_color(color):
# This function changes the style to all buttons using the "color.Button style"
if color == "red":
color_style_button.configure("color.TButton", foreground="red")
elif color == "blue":
color_style_button.configure("color.TButton", foreground="blue")
elif color == "green":
color_style_button.configure("color.TButton", foreground="green")
frame_a = ttk.Frame(root)
frame_a.pack()
red_button = ttk.Button(frame_a, text="Red", command=lambda: change_color("red"), style="color.TButton")
red_button.pack()
blue_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("blue"), style="color.TButton")
blue_button.pack()
green_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("green"), style="color.TButton")
green_button.pack()
root.mainloop()
</code></pre>
<p>我建议查看<a href="http://www.tkdocs.com/tutorial/styles.html" rel="nofollow" title="Styles and Themes">this site</a>以了解有关ttk和样式的更多信息。</p>