Python:更改框架root.configure(background ='red')的颜色不起作用

时间:2016-05-25 12:26:10

标签: python tkinter background-color

我在tkinter中创建了一个多窗口GUI,我想改变gui的backgroundcolor。我尝试了多种建议的解决方案但它们不起作用。例如root.configure(background ='red')。这也不起作用:http://www.java2s.com/Code/Python/GUI-Tk/SetFramebackgroundcolor.htm

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT = ("Verdana", 12)

def loadmsg(msg):
    popupload.fileName = filedialog.askopenfilename(filetypes=(("textfiler", ".txt"), ("Alla filer", ".*")))


class Dod(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "GUi")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()



class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="GUI", font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        button = ttk.Button(self, text="Visit Page One",
                            command=lambda: controller.show_frame(PageOne))


        button2 = ttk.Button(self, text="Ok",
                             command=lambda: controller.label2)

        button.pack()
        button2.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        button1 = ttk.Button(self, text="Back to Home",
                             command=lambda: controller.show_frame(StartPage))

        button1.pack()

root = Dod()
root.geometry("800x400")
root.mainloop()

1 个答案:

答案 0 :(得分:0)

这很容易......

您需要做的就是在参数container.grid_columnconfigure(0,weight = 1)之后在Dod(tk.Tk)类的 init 函数中添加一个container.config()参数。然后,在循环内的self.frames [F] = frame之后添加frame.config(bg =“black”),用于F in(start1,start,P1,P2)。[这将为循环中的所有帧提供黑色] 代码如下:

import tkinter as tk
from tkinter import *
from tkinter import ttk

LARGE_FONT = ("Verdana", 12)

def loadmsg(msg):
    popupload.fileName = filedialog.askopenfilename(filetypes=(("textfiler",    ".txt"), ("Alla filer", ".*")))


class Dod(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "GUi")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        F1.config()

        self.frames = {}
        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.config(bg="black")
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()



class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="GUI", font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        button = ttk.Button(self, text="Visit Page One",
                        command=lambda: controller.show_frame(PageOne))


        button2 = ttk.Button(self, text="Ok",
                         command=lambda: controller.label2)

        button.pack()
        button2.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page One", font=LARGE_FONT)
        label.pack(pady=10, padx=10)
        button1 = ttk.Button(self, text="Back to Home",
                         command=lambda: controller.show_frame(StartPage))

        button1.pack()

root = Dod()
root.geometry("800x400")
root.mainloop()