在Tkin中更改多个窗口的背景

2024-09-28 23:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的程序中做一个设置菜单,这样你就可以更改程序中所有窗口的背景。但我不知道怎么做,当你点击按钮时,背景会改变。有什么帮助吗?如果需要的话,以下是我目前所掌握的信息:

    #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()

不多,我也试过一些方法,但都没用。在


Tags: lambdatextselfinitshowbuttonframecommand
1条回答
网友
1楼 · 发布于 2024-09-28 23:28:54

有两种解决方案:保留对每个窗口的引用并使用configure方法更改背景,或者创建一个函数,在更改某些全局值后重新创建整个UI。在

以下是第一种方法的大致概述:

class ControllerClass(object):
    def __init__(self):
        ...
        self.windows = []
        ...
    def show_frame(self, frame_class):
        ...
        the_frame = frame_class(root, self)
        self.windows.append(the_frame)
        ...
    def change_color(self):
        ...
        for frame in self.windows:
            frame.configure(background=the_color)
        ...

当然,它应该更复杂一点。例如,您的控制器可能有一个“设置”字典,而不是单一颜色。另外,您可以考虑让每个窗口对象负责更改其自身的颜色,所以您可以frame.set_color(the_color)。这样,每个窗口不仅可以设置自身的背景,还可以设置任何相关的子窗口。在

相关问题 更多 >