简体   繁体   English

Python Tkinter改变框架背景颜色貌似没有效果

[英]Python Tkinter changing frame background colour seemingly has no effect

I'm rather new to Tkinter and am attempting to put a frame within a frame, which I have successfully done, but when I attempt to change the background colour of the parent frame it has no effect.我对 Tkinter 比较陌生,并试图将框架放入框架中,我已经成功完成了,但是当我尝试更改父框架的背景颜色时,它没有任何效果。

My code is as follows:我的代码如下:

import tkinter as tk

class card_game_gui:

    def __init__(self, root):
        self.pagenum = 1
        self.root = root

    def page1(self):
        page = tk.Frame(self.root, bg="blue") #<-- this line has seemingly no effect
        form = tk.Frame(page, bg="white")
        self.root.grid_columnconfigure(0, weight=1)

        tk.Label(self.root, text='Add player', bg="dark gray").grid(row=0, column=0, columnspan=2, sticky="NESW")

        self.user = tk.Entry(form, bd=0, bg="light gray")
        self. user.bind("<FocusIn>", self.UserEntryFocusIn)
        self.user.bind("<FocusOut>", self.UserEntryFocusOut)
        self.user.grid(row=1, column=1, sticky="NSEW", pady=5)

        user_label = tk.Label(form, text="USERNAME:", bg="white")
        user_label.grid(row=1, column=0, sticky="NSEW")

        self.pw = tk.Entry(form, bd=0, bg="light gray", show="*")
        self.pw.bind("<FocusIn>", self.PwEntryFocusIn)
        self.pw.bind("<FocusOut>", self.PwEntryFocusOut)
        self.pw.grid(row=2, column=1, sticky="EW", pady=5)

        pw_label = tk.Label(form, text="PASSWORD:", bg="white")
        pw_label.grid(row=2, column=0, sticky="NSEW")

        tk.Button(form, text='To page 2', command=self.changepage).grid(row=3, column=0)

        page.grid(pady=50, padx=50)
        form.grid()

    def UserEntryFocusIn(self, event):
        self.user.config(bg="white")

    def UserEntryFocusOut(self, event):
        self.user.config(bg="light gray")

    def PwEntryFocusIn(self, event):
        self.pw.config(bg="white")

    def PwEntryFocusOut(self, event):
        self.pw.config(bg="light gray")

    def page2(self):
        root = self.root

        page = tk.Frame(root)
        page.grid()
        tk.Label(page, text = 'This is page 2').grid(row = 0)
        tk.Button(page, text = 'To page 1', command = self.changepage).grid(row = 1)

    def changepage(self):
        root = self.root

        for widget in root.winfo_children():
            widget.destroy()
        if self.pagenum == 1:
            self.page2()
            self.pagenum = 2
        else:
            self.page1()
            self.pagenum = 1

root = tk.Tk()
card_game_gui = card_game_gui(root)
card_game_gui.page1()
root.mainloop()

Expected output (The blue scribbled upon area represents where the background should be, but isn't blue)预期输出(蓝色涂鸦区域表示背景应该在哪里,但不是蓝色)

预期输出

Actual output实际产量

实际产量

The frame page to which you have assigned blue color is completely packed with the child frame form which has background color white .您指定为blue的框架page完全包含背景颜色为white的子框架form The area that you want to be blue is not a frame instead is the master window area from the padding to the frame page .您想要变成蓝色的区域不是框架,而是从填充到框架page的主窗口区域。 So you need to configure the color of master window to blue .所以你需要将主窗口的颜色配置为blue

import tkinter as tk

class card_game_gui:

    def __init__(self, root):
        self.pagenum = 1
        self.root = root

        #<-- configure color of your master window
        self.root.configure(background='blue')

    def page1(self):
        page = tk.Frame(self.root, bg="blue") #<-- this line has seemingly no effect
        form = tk.Frame(page, bg="white")
        self.root.grid_columnconfigure(0, weight=1)

        tk.Label(self.root, text='Add player', bg="dark gray").grid(row=0, column=0, columnspan=2, sticky="NESW")

        self.user = tk.Entry(form, bd=0, bg="light gray")
        self. user.bind("<FocusIn>", self.UserEntryFocusIn)
        self.user.bind("<FocusOut>", self.UserEntryFocusOut)
        self.user.grid(row=1, column=1, sticky="NSEW", pady=5)

        user_label = tk.Label(form, text="USERNAME:", bg="white")
        user_label.grid(row=1, column=0, sticky="NSEW")

        self.pw = tk.Entry(form, bd=0, bg="light gray", show="*")
        self.pw.bind("<FocusIn>", self.PwEntryFocusIn)
        self.pw.bind("<FocusOut>", self.PwEntryFocusOut)
        self.pw.grid(row=2, column=1, sticky="EW", pady=5)

        pw_label = tk.Label(form, text="PASSWORD:", bg="white")
        pw_label.grid(row=2, column=0, sticky="NSEW")

        tk.Button(form, text='To page 2', command=self.changepage).grid(row=3, column=0)

        page.grid(pady=50, padx=50)
        form.grid()

    def UserEntryFocusIn(self, event):
        self.user.config(bg="white")

    def UserEntryFocusOut(self, event):
        self.user.config(bg="light gray")

    def PwEntryFocusIn(self, event):
        self.pw.config(bg="white")

    def PwEntryFocusOut(self, event):
        self.pw.config(bg="light gray")

    def page2(self):
        root = self.root

        page = tk.Frame(root, bg='blue')
        page.grid()
        tk.Label(page, text = 'This is page 2').grid(row = 0)
        tk.Button(page, text = 'To page 1', command = self.changepage).grid(row = 1)

    def changepage(self):
        root = self.root

        for widget in root.winfo_children():
            widget.destroy()
        if self.pagenum == 1:
            self.page2()
            self.pagenum = 2
        else:
            self.page1()
            self.pagenum = 1

root = tk.Tk()
card_game_gui = card_game_gui(root)
card_game_gui.page1()
root.mainloop()

PAGE 1第 1 页

The blue area is actually the master window (root window) region from the padding value of 50 (padx and pady) to the frame page .蓝色区域实际上是主窗口(根窗口)区域,从 padding 值为 50(padx 和 pady)到框架page

乙

PAGE 2第2页

Since you haven't given any padding to page 2 widgets, you don't have such a large master window area.由于您没有为第 2 页小部件提供任何填充,因此您没有如此大的主窗口区域。

第2页

I have explained the changes with some theory.我已经用一些理论解释了这些变化。 I hope you understand!我希望你明白!

The reason that you don't see the change is that 'page' is hidden behind 'form'.您看不到更改的原因是“页面”隐藏在“表单”后面。 If you want that area to be blue, try adding this line after 'root = tk.Tk()'.如果您希望该区域为蓝色,请尝试在“root = tk.Tk()”之后添加此行。

root.config (bg = 'blue')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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