Shraneid
Shraneid

Reputation: 408

Tkinter grid is resetting the background color

So I'm beginning with tkinter and there is a problem I cannot solve, when I use the grid to define where I want my stuff to be, every cell in the grid will not have the desired background color.

Better to show than to explain too much :

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

So as you can see, from here : screen

The background inside the grid is resetted and I have added background modifiers everywhere I could, I still cannot get it to work

PS : I know I should uncomment the 2 lines in the beginning for it to work like I want but this is just to show that the frame background is working but the grid one is not the same for some reason

Thanks in advance, Shraneid

Upvotes: 0

Views: 767

Answers (2)

Mike - SMT
Mike - SMT

Reputation: 15226

The background inside the grid is resetted and I have added background modifiers everywhere I could, I still cannot get it to work

The background is not resetting. The frame contains its own background color so you need to tell your MainPage class (aka the frame) what color you want it to be.

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

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386210

It is not possible for grid to change the color of anything. grid isn't a "thing" and doesn't itself have a color or change the color of anything. If you want the background of MainPage to be black, you need to set it to black.

If you want it hard-coded, just add it when calling the __init__ method of the superclass:

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

Upvotes: 2

Related Questions