我正在尝试在我的程序中做一个设置菜单,这样你就可以更改程序中所有窗口的背景。但我不知道怎么做,当你点击按钮时,背景会改变。有什么帮助吗?如果需要的话,以下是我目前所掌握的信息:
#Settings
class programSettings(tk.Frame):
#Initialize
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Setups
title = ttk.Label(self, text = "Settings", font = LARGE_FONT)
colorButton = ttk.Button(self, text = "Background Color", command = lambda: controller.show_frame(color))
menuButton = ttk.Button(self, text = "Main Menu", command = lambda: controller.show_frame(StartPage))
#Placement
title.pack()
colorButton.pack()
menuButton.pack()
#Color
class color(tk.Frame):
#Initialize
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Setups
blueButton = ttk.Button(self, text = "Blue", command = lambda: controller.show_frame(programSettings))
blueButton.configure(bg = "#4285F4")
#Placement
blueButton.pack()
不多,我也试过一些方法,但都没用。在
有两种解决方案:保留对每个窗口的引用并使用
configure
方法更改背景,或者创建一个函数,在更改某些全局值后重新创建整个UI。在以下是第一种方法的大致概述:
当然,它应该更复杂一点。例如,您的控制器可能有一个“设置”字典,而不是单一颜色。另外,您可以考虑让每个窗口对象负责更改其自身的颜色,所以您可以
frame.set_color(the_color)
。这样,每个窗口不仅可以设置自身的背景,还可以设置任何相关的子窗口。在相关问题 更多 >
编程相关推荐