Cannot set a background color for my tkinter application?

When I try to set a background color for my main Frame, that has all widgets as childs, it only change the very bottom of the background. If I set a background for all Frame widgets, it still does not color some empty space. How can I set a background color for it ? Here’s the result with Frames colored.

enter image description here

The runnable code:

import tkinter as tk

class ToolbarButton(tk.Button):

    def __init__(self, master, text, pixelref, *args, **kw):
        self.configure(text=text, image=pixelref, height=20, width=20, compound='center')

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs, bg="red")
        self.parent = parent

        # Textframe
        self.text_frame = tk.Frame(root, width=600, height=790, bg="green") #doesn't show: has text_widget over
        self.text_widget = tk.Text(self.text_frame, width=1, height=1)
        self.text_widget.pack(expand=True, fill='both')

        # Toolbar
        self.toolbar = tk.Frame(root,bg="blue")
        self.pixel = tk.PhotoImage(width=1, height=1)

        self.bold_button = ToolbarButton(self.toolbar, 'B', self.pixel)
        self.bold_button.pack(side='left', padx=4)
        self.italic_button = ToolbarButton(self.toolbar, 'I', self.pixel)
        self.italic_button.pack(side='left', padx=4)
        self.underline_button = ToolbarButton(self.toolbar, 'U', self.pixel)
        self.underline_button.pack(side='left', padx=4)

        # Packing
        self.toolbar.pack(side='top', pady=60)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)


You have placed your text_frame widget on the root, instead of on the application frame. I think you want self there instead of root. That makes it behave as I expect.

    self.text_frame = tk.Frame(self, width=600, height=790, bg="green") #doesn't show: has text_widget over

Leave a Reply

Your email address will not be published. Required fields are marked *