我有一些代码(如下所示)提示用户选择要将GUI更改为的颜色。但我的问题是它只会改变背景。我想知道是否有办法一次更改每个标签和按钮的背景,或者我必须单独更改每个标签/按钮。
import tkinter
window = tkinter.Tk()
colour_frame = tkinter.Frame(window)
options_frame = tkinter.Frame(window)
def colours():
options_frame.pack_forget()
red.pack()
orange.pack()
back_button.pack()
colour_frame.pack()
def back():
options_frame.pack()
colour_frame.pack_forget()
def make_red():
window.configure(background="red")
def make_orange():
window.configure(background="orange")
colour_button = tkinter.Button(options_frame, text="Appearance", command=colours)
red = tkinter.Button(colour_frame, text="RED", command=make_red)
red.configure(bg = "red")
orange = tkinter.Button(colour_frame, text="ORANGE", command=make_orange)
orange.configure(bg = "orange")
back_button = tkinter.Button(colour_frame, text="Back", command=back)
window.mainloop()
尝试对一些GUI元素使用ttk。ttk允许您为小部件创建样式,并一次将样式更新为所有小部件(至少对于具有相同样式的小部件)。您可能需要混合使用ttk和tkinter,但从长远来看,这应该会使事情变得简单一些。我举了一个例子:
我建议查看this site以了解有关ttk和样式的更多信息。
您可以创建一个包含所有要更改的小部件的列表
下面是一个同时改变多个标签背景颜色的示例代码。
或者如果您有一个框架已经包含了您想要更改的所有小部件,那么您可以使用它来代替。
parent_widget.winfo_children()
将返回一个包含父小部件中存储的所有小部件的列表相关问题 更多 >
编程相关推荐