简体   繁体   中英

How to change the background color in this example

I have been playing around with this example code just trying different things. I think Bryan Oakley originally wrote this. Given this example code how can I change the gray area, what I believe is the window background, to black? It looks to me that the window is using the standard window color of gray. I have changed every Frame in an attempt to figure out each area and how they are displayed. I have also added the root background tag with no luck. This is driving me a little nuts.

import Tkinter as tk
from Tkinter import *


class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()


class Page1(Page):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        newframe = tk.Frame(self, width=780, height=540, background="red")
        newframe.place(anchor="c", relx=.5, rely=.5)

class Page2(Page):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        newframe = tk.Frame(self, width=780, height=540, background="blue")
        newframe.place(anchor="c", relx=.5, rely=.5)


class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)

        buttonframe = tk.Frame(self, bg='green')
        container = tk.Frame(self, bg='yellow')
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        self.b1 = tk.Button(buttonframe, text="Page 1", command=lambda: p1.lift())
        self.b1.pack(side="left")
        self.b2 = tk.Button(buttonframe, text="Page 2", command=lambda: p2.lift())
        self.b2.pack(side="left")

        p1.show()


if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    root.configure(bg='black')
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("800x600")
    root.mainloop()

I don't really know why this works but here is what I did. Inside the MainView change the two p1, p2 code to the following.

    p1 = Page1(self, bg='black')
    p2 = Page2(self, bg='black')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM