Why does packing stuff make the frames smaller in Python
If you look closely, the blue and yellow frames are smaller and the pink is bigger when the text is added. Why does this happen and is there any way to solve this problem?
When you use weight, it does not define the ratio of all space to give to each column. It defines how much of the extra space is given to each column. Since none of your widgets have a defined size, they all expand according to the rules you've set forth so it appears that the weight defines the actual width.
When you add a widget to the center section, that label requires a certain amount of space, and thus the center frame now requires a certain amount of space. That means that there is less extra space to be allocated, and this extra space will be added to the space already being used by the label.
When you start out, none of the frames have a width. Let's say we end up setting the width of the window to 900 pixels. Because of the weights, this 900 pixels is split up into 200 pixels on the left, 500 in the middle, and 200 on the right, like you see in your first picture.
Now, let's say that the middle label is added and it takes up 100 pixels. That means there are now 800 extra pixels that have to be distributed, instead of 900. The left and right columns each get 2/9ths of 800 or about 178 pixels. The center section gets 5/9th of that extra space or about 444 pixels. These 444 pixels are added to the existing 100 pixels required by the middle, resulting in a middle section that gets 544 pixels. Thus, the side columns are narrower and the middle column is wider than when there was nothing in the center.
As to the fix, it's hard to say. The fix for this specific case where you have nothing in the left and right is going to be different than the case where you have widgets in the left and right.
As a general rule, don't depend on the weight
to define the actual widths, because that's not what it is for. Design your GUI by giving minimum widths to the widgets inside the columns and letting tkinter compute the actual widths. Then, the weight can be used to decide how to distribute extra space when the window grows. Alternately, you can use the minsize
attribute of the columnconfigure
command to force the left and right sizes to have a minimum width.