5

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?

2
  • I run your code and it seems to be working. screenshoit. Can you post your screenshot so that we know what you it does not work.
    – Marcin
    Feb 13, 2015 at 5:45
  • I accidentally left in the hard-coded width=200 for the content_frame. This makes it appear to work correctly until you resize the window and see that it isn't flexing, but remains hard-coded to 200 px wide. I've edited the code to better illustrate the problem. This is what I see: link
    – Foswick
    Feb 13, 2015 at 5:57

1 Answer 1

7

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()
3
  • 1
    Brilliant! Works perfectly. Not entirely sure why it does, but it does. At least it shows me where I need to read up to learn why. Thank you!
    – Foswick
    Feb 13, 2015 at 6:15
  • doesn't work on another example. no explanation :( Please can you add why?
    – sol
    Apr 26, 2021 at 8:23
  • @sol, I don't understand what you mean. You'd better to post a separate question to get higher chance of answers.
    – falsetru
    Apr 26, 2021 at 13:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.