Python Tkinter改变框架背景颜色貌似没有效果 [英] Python Tkinter changing frame background colour seemingly has no effect

查看:55
本文介绍了Python Tkinter改变框架背景颜色貌似没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Tkinter 比较陌生,正在尝试将框架放入框架中,我已经成功完成了,但是当我尝试更改父框架的背景颜色时,它没有任何效果.

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.

我的代码如下:

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)

实际输出

推荐答案

您指定了 blue 颜色的框架 page 与子框架 完全挤在一起>form 背景色为 white.您想要变成蓝色的区域不是框架,而是从填充到框架 page 的主窗口区域.所以你需要将主窗口的颜色配置为blue.

The frame page to which you have assigned blue color is completely packed with the child frame form which has background color white. 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. So you need to configure the color of master window to 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()

第 1 页

蓝色区域实际上是从padding值为50(padx和pady)到框架page的主窗口(根窗口)区域.

The blue area is actually the master window (root window) region from the padding value of 50 (padx and pady) to the frame page.

第 2 页

由于您没有为第 2 页小部件提供任何填充,因此您没有如此大的主窗口区域.

Since you haven't given any padding to page 2 widgets, you don't have such a large master window area.

我已经用一些理论解释了这些变化.希望你理解!

I have explained the changes with some theory. I hope you understand!

这篇关于Python Tkinter改变框架背景颜色貌似没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆