Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter grid正在重置背景色_Python_User Interface_Tkinter_Background - Fatal编程技术网

Python Tkinter grid正在重置背景色

Python Tkinter grid正在重置背景色,python,user-interface,tkinter,background,Python,User Interface,Tkinter,Background,所以我从tkinter开始,有一个我无法解决的问题,当我使用网格定义我想要的东西的位置时,网格中的每个单元格都不会有想要的背景颜色 与其解释太多,不如展示: import tkinter as tk from tkinter import ttk LARGE_FONT = ("arial", 20) class Main(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args,

所以我从tkinter开始,有一个我无法解决的问题,当我使用网格定义我想要的东西的位置时,网格中的每个单元格都不会有想要的背景颜色

与其解释太多,不如展示:

import tkinter as tk
from tkinter import ttk

LARGE_FONT = ("arial", 20)

class Main(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        #self.grid_rowconfigure(0, weight=1) 
        #self.grid_columnconfigure(0, weight=1)
        self.configure(background='black')

        main_container = tk.Frame(self)
        main_container.grid(column=0, row=0, sticky = "nsew")
        main_container.grid_rowconfigure(0, weight = 1)
        main_container.grid_columnconfigure(0, weight = 1)

        main_container.configure(background="black")

        menu_bar = tk.Menu(main_container)
        file_menu = tk.Menu(menu_bar, tearoff = 0) 
        file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
        file_menu.add_separator()
        file_menu.add_command(label = "Exit", command = quit)
        menu_bar.add_cascade(label = "File", menu = file_menu)

        tk.Tk.config(self, menu = menu_bar)

        self.frames = {}

        for fr in (MainPage,):
            frame = fr(main_container, self)
            self.frames[fr] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
        self.show_frame(MainPage)

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

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.columnconfigure(0, weight = 1)
        self.columnconfigure(1, weight = 1)
        self.rowconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)

        label = tk.Label(self, text = "Main Page", fg = "white", bg="black", font = LARGE_FONT)
        label.grid(row = 0, column = 0, padx = 10, pady = 10)

        label2 = tk.Label(self, text = "Main Page2", fg = "white", bg="black", font = ("Arial",8))
        label2.grid(row = 1, column = 0, padx = 10, pady = 10, sticky = 'ne')

        button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
        button2.grid(row = 0, column = 1, sticky = 'nswe')

        button3 = ttk.Button(self, text = "Exit", command = quit)
        button3.grid(row = 1, column = 1, sticky = 'nswe')

app = Main()
app.geometry("1280x720")
app.mainloop()
如你所见,从这里:

网格内的背景被重置,我在任何地方都添加了背景修改器,但仍然无法使其工作

PS:我知道我应该在开始时取消注释这两行,让它像我想要的那样工作,但这只是为了显示帧背景工作正常,但网格背景由于某些原因不一样

提前感谢,,
Shraneid网格不可能更改任何东西的颜色<代码>网格不是一个“东西”,它本身没有颜色,也不会改变任何东西的颜色。如果希望
MainPage
的背景为黑色,则需要将其设置为黑色

如果要硬编码,只需在调用超类的
\uuuuu init\uuuuu
方法时添加它:

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background="black")

grid
无法更改任何东西的颜色<代码>网格不是一个“东西”,它本身没有颜色,也不会改变任何东西的颜色。如果希望
MainPage
的背景为黑色,则需要将其设置为黑色

如果要硬编码,只需在调用超类的
\uuuuu init\uuuuu
方法时添加它:

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background="black")
网格内的背景被重置,我在任何地方都添加了背景修改器,但仍然无法使其工作

背景没有重置。框架包含自己的背景色,因此您需要告诉您的
MainPage
类(也称框架)您希望它是什么颜色

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        # Add this line here
        self.configure(background='black')
网格内的背景被重置,我在任何地方都添加了背景修改器,但仍然无法使其工作

背景没有重置。框架包含自己的背景色,因此您需要告诉您的
MainPage
类(也称框架)您希望它是什么颜色

class MainPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        # Add this line here
        self.configure(background='black')

将此行添加到您的
主页
self.configure(background='black')
将此行添加到您的
MainPage
类中<代码>self.configure(background='black')
哦,好的,我以为它是从主类继承了它的背景,谢谢@Shraneid:
MainPage
tk.Frame
继承其背景。
tk.Frame
的默认背景色为灰色。哦,好的,我以为它是从主类继承了它的背景,谢谢@Shraneid:
MainPage
tk.Frame
继承其背景。
tk.Frame
的默认背景色为灰色。哦,好的,我以为它是从主类继承了它的背景,谢谢!哦,好吧,我还以为它是从主类继承了它的背景呢,谢谢!