2

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):
        super().__init__(master)
        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_frame.pack_propagate(False)
        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)
        self.text_frame.pack(expand=True)


if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
2
  • Have you tried <any tkinter widget>.config(background="black") Commented Mar 15, 2021 at 21:44
  • @TheLizzard Yes, and as expected it leads to the same results Commented Mar 15, 2021 at 21:48

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I was completely blind to this mistake, having the root word in habits...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.