Slayther Slayther - 1 month ago 6
Python Question

Second frame with fill both and expand true not expanding fully

I have 2 frames in a column. The top frame should fill

and be fixed
. This works good. The bottom frame should fill the remaining space, but setting
doesn't seem to work the way I expected it to. The bottom frame expands, but not fully, leaving gray background of the root between the frames.

Here is minimum code to reproduce the problem:

import Tkinter as tk

if __name__ == "__main__":
root = tk.Tk()

top_frame = tk.Frame(root, height=50, bg="blue")
top_frame.pack(anchor="n", fill="x", expand=True)

bot_frame = tk.Frame(root, bg="red")
bot_frame.pack(anchor="n", fill="both", expand=True)


Ideally, the bottom frame should also start with a minimum size, but I will probably be able to figure it out once this problem is solved.


expand and fill are completely independent of each other. expand answers the question "do I get extra space?", and fill answers the question "how do I use the extra space that was given to me?".

So, you have two frames both of which have expand=True. That means that tkinter will give half of the extra space to one frame, and half the extra space to the other, regardless of how those widgets plan to use the extra space.

Since the top frame only fills in the X direction, the extra space it has been given in the Y direction goes unused. That is why it appears gray below the defined height of the frame.

The solution to this specific problem is to have expand be false for the top frame because you do not want it to be given extra space.