Python – Tkinter Frame Doesn’t Fill Remaining Space

pythontktkinter

This is my first encounter with Tkinter, and I'm beating my head against a wall over this.

Ultimately, I am wanting a layout that uses a Frame along the left edge that contains a stack of buttons (navigation). This should be hard-wired to 100 px wide and take up all vertical space. The rest of the window should be filled with a content Frame, the contents of which I'll change dynamically.

So, should be simple. Here's the simplified version of what I have (color-coded for reference):

from Tkinter import *

root = Tk()
root.geometry("300x200")

navbar = Frame(root, bg="green", width=100)
navbar.pack(anchor=W, fill=Y, expand=True, side=LEFT)

content_frame = Frame(root, bg="orange")
content_frame.pack(anchor=N, fill=BOTH, expand=True, side=LEFT )

root.mainloop()

What I would expect to see is a green bar (100 px wide) on the left side of the window (full height), and the rest of the window orange. However, what I get is the Irish flag (green, white, orange).

No matter what I do, the orange content_frame just refuses to fill horizontally. It seems as though Tk has divided the window exactly in half, placing the green navbar on the left side (as expected), but giving the orange content_frame only half of the remaining space.

I can force the orange to fill the space by coding it, in this case to width=200, but this does not flex when the window resizes. Also, if I don't add the green navbar, the orange content_frame happily fills the whole window.

This example code shows the content_frame with anchor=N, but it doesn't matter where I anchor it. It also doesn't matter whether I side=LEFT or side=RIGHT.

I also discovered that no matter how I size the width of the green navbar, Tk always divides the remaining horizontal space in half, and places the orange content_frame in the right half.

This has to be simple. Any ideas?

Best Solution

specify expand=False for the left frame:

from Tkinter import *

root = Tk()
root.geometry("300x200")

navbar = Frame(root, bg="green", width=100)
navbar.pack(anchor=W, fill=Y, expand=False, side=LEFT)  # <----

content_frame = Frame(root, bg="orange")
content_frame.pack(anchor=N, fill=BOTH, expand=True, side=LEFT )

root.mainloop()
Related Question