In this post we will learn in depth on the Pack() function in Python Tkinter. Lets get started,
By default, when a window is created the created will of 400x200 height and weight. Which can be altered to our desired height and weight using the geometry(), the parameters has to be passed a string within quotes.
To experiment operation on pack() we will use two labels of different color, In above in order the distinguish the text inside the label we pass parameter ipadx and ipady which will provide top/bottom and right/left spacing to the text inside the label.
Now will use fill = x in the Rectangle 1, since the side is top the Rectangle 1 is reserved horizontally and with fill = x the entire row of the label will be filled in green.
With fill = y in the Rectangle 1, side is top the Rectangle 1 is reserved vertically and with fill = y the entire column of the label will be filled in green in centered. This will not hide the label Rectangle 2 which is located in the bottom of Rectangle 1.
With fill = both in the Rectangle 1, side is top the Rectangle 1 is reserved vertically and with fill = both the entire row and column of the label will be filled in green.
With expand = True and no fill in the Rectangle 1, side is top the Rectangle 1 when expanded the Rectangle 1 will be centered when the window is expanded from any of the sides.
With fill = both in the Rectangle 1, side is top the Rectangle 1 is reserved vertically and with fill = both the entire row and column of the label will be filled in green. With expand = True and no fill in the Rectangle 2, side is top the Rectangle 2 when expanded the Rectangle 1 will be centered when the window is expanded from any of the sides. On observing closely you will notice that for both labels space will be served equally i.e, 50 - 50.
Since the first label's side is left the fill is both, priority is given to the Rectangle 1 then to the Rectangle 2 with side = top.
Since the first label's side is left the fill is both, priority is given to the Rectangle 1 then to the Rectangle 2 with side = top. Reserved space of the rectangle will continue to exist despite the expand for Rectangle 2.
This above scenario is quite different, here we have 3 Rectangles with side of Rectangle 1 and Rectangles 3 is left and Rectangles is top. The 1st priority to fill is given to both Rectangles 1 and 3 then to 2.
Points to Remember
Finally, side values
We can specify which side to anchor components on.
The default is side = "top"
When side = "top" or side = "bottom", widgets take up all the horizontal space
When side = "left" or side = "right", widgets take up all the vertical space
Widgets are pushed in the direction of their anchor point ass far as possible within the available space.
Expansion priority
This causes some very interesting things to happen add more widgets into the mix
For example, first side = "left", then side = "top", then another side = "left" all with expand = True
The side = "left" ones get expansion priority because the first widget added with expand = True had side value
Pack is weird, but its good. It takes time to fully understand what' going to happen when you use pack. But when you do, pack is really weird and easy to use. For more complicated layouts, there are other algorithms that can be more suitable, such as grid