pack

The pack geometry manager acts based on the concept of using up free space within the parent widget. When packing, you can specify at which end of the free space to put the widget, and how it will grow along with said free space (as the window itself grows and shrinks). The geometry manager than assigns widgets into said free space, leaving as little empty space as possible.

The pack geometry manager is primarily controlled by three keyword arguments:

  • side: On which end of the available space do you want to place the widget? The options are defined as constants within Tkinter, as LEFT, RIGHT, TOP, and BOTTOM.
  • fill: Do you want the widget to fill any available space around it? The options are also constants: X or Y. These are Cartesian, meaning X is horizontal and Y is vertical. If you want the widget to expand in both directions, use the BOTH constant.
  • expand: Should the widget resize when the window does? This argument is a Boolean, so you can pass True or 1 to make the widget grow with the window.

These are not the only arguments that can be provided to pack; there are others which handle things such as spacing, but these are the main ones you will use. The pack geometry manager is somewhat difficult to explain, but tends to create very readable code thanks to its use of words to describe positions.

The order in which widgets are packed matters greatly. Suppose you have two buttons which you wish to stack vertically, with one underneath the other. The first button, which you call pack(side=tk.BOTTOM) on, will be at the very bottom of the main window. The next widget, which is packed with side=tk.BOTTOM, will then appear above it. Bear this in mind if your widgets appear to be out of order when using pack as your geometry manager.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset