簡體   English   中英

如何在此示例中更改背景顏色

[英]How to change the background color in this example

我一直在玩這個示例代碼只是嘗試不同的東西。 我認為布萊恩奧克利最初是這樣寫的。 鑒於此示例代碼,如何將灰色區域(我認為是窗口背景)更改為黑色? 在我看來,窗口使用的是標准窗口顏色的灰色。 我已經更改了每個Frame,試圖找出每個區域以及它們的顯示方式。 我還添加了根背景標簽,沒有運氣。 這讓我有點瘋狂。

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

我真的不知道為什么會這樣,但這就是我所做的。 在MainView內部將兩個p1,p2代碼更改為以下內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM