【问题标题】:Change background color in python using tkinter使用 tkinter 在 python 中更改背景颜色
【发布时间】:2023-04-10 04:50:01
【问题描述】:

嘿,我正在尝试使用 tkinter 更改我的 python 应用程序的背景颜色。 我试过了,但它不起作用。

app.configure(background='#FBC311')

我明白了:

我想要填充整个背景。这是我的代码:

import tkinter as tk
from tkinter import *


class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)



        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        for row in range(7):
            container.grid_rowconfigure(row, weight=1)
        for column in range(7):
            container.grid_columnconfigure(column, weight=1)

        self.frames = {}
        for F in (StartPage, Departure, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Welkom bij NS", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.grid(row=0,column=2,columnspan=4)

        button1 = tk.Button(self, text="Actuele reistijden", command=lambda: controller.show_frame("Departure"),bg='#005ca0', fg='white',width=20,height=5)
        button1.grid(row=2, column=2,sticky='nsew')
        button2 = tk.Button(self, text="Go to Page Two", command=lambda: controller.show_frame("PageTwo"),bg='#005ca0', fg='white',width=20,height=5)
        button2.grid(row=2,column=4)

class Departure(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Actuele vertrektijden", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Start",command=lambda: controller.show_frame("StartPage"),bg='#005ca0', fg='white',width=20,height=5)
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Storingen", font='Verdana 50', fg='#005ca0', bg='#FBC311')
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Start", command=lambda: controller.show_frame("StartPage"), bg='#005ca0', fg='white',width=20,height=5)
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.configure(background='#FBC311')
    app.mainloop()

【问题讨论】:

  • 我想指出的是,您实际上并没有询问如何更改应用程序的背景颜色,而只是询问了应用程序中的一些小部件。

标签: python tkinter


【解决方案1】:

在框架的for循环中,为背景添加一行:

for F in (StartPage, Departure, PageTwo):
    page_name = F.__name__
    frame = F(parent=container, controller=self)
    self.frames[page_name] = frame
    frame.config(bg="black") # or whatever color you like

【讨论】:

    猜你喜欢
    • 2016-09-10
    • 2020-10-09
    • 2013-03-21
    • 2017-10-16
    • 2018-10-01
    • 1970-01-01
    • 2016-09-11
    • 2012-07-31
    • 2020-02-10
    相关资源
    最近更新 更多