Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 40 ยท Layout Managers

The Tkinter pack() Layout Manager

Master Tkinter's pack geometry manager: side, fill, expand, anchor, padding and ipadding, packing order, the cavity model and typical toolbar and sidebar layouts.

Creating a widget does not put it on screen. A geometry manager decides where it goes and how big it is. Tkinter has three: pack, grid and place. pack is the easiest: it stacks widgets against the sides of their parent, one after another. For rows of buttons, toolbars, sidebars and simple vertical forms it is often all you need.

The basic idea

Python
import tkinter as tk

root = tk.Tk()
tk.Label(root, text="One", bg="#e76f51").pack()
tk.Label(root, text="Two", bg="#2a9d8f").pack()
tk.Label(root, text="Three", bg="#264653", fg="white").pack()
root.mainloop()

With no options, pack() places each widget at the top of the space that is left, centred horizontally. The window shrinks to fit. Each call claims a strip of the remaining space, in the order the calls are made. That order matters, so it deserves a model.

The cavity model

Think of the parent as a rectangular cavity. For each widget, in packing order:

  1. Cut a parcel off the chosen side of the cavity (the full width for top/bottom, the full height for left/right).
  2. Place the widget inside its parcel (anchor, fill, padding).
  3. The cavity is now smaller by the size of the parcel.
flowchart TB
  A["Cavity: whole window"] --> B["pack side=top: strip at top"]
  B --> C["Cavity shrinks"]
  C --> D["pack side=left: strip at left of what remains"]
  D --> E["Cavity shrinks again"]

Each pack call carves a parcel from the remaining cavity, which is why the order of calls changes the result.

The stand-in below implements this rule for stacks of widgets (top, bottom, left, right with fill and expand). It is a simplified teaching model, not Tk's code, but its answers match Tk for these cases and show you how the options interact.

Python Stand-in: the pack cavity algorithm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the printed boxes: the header and footer span the whole width, the sidebar takes the left strip between them, and content (the expanding widget) soaks up all leftover space. Notice the order: the footer was packed before the sidebar and content, so it gets the full window width. Had content (which expands to all the remaining width) been packed first, the cavity would have no width left and the footer would end up with a zero-width parcel, which is the real Tk behaviour too: widgets packed after a greedy one get squeezed out. Pack order decides who gets the corners. Edges first, the expanding widget last.

The options

Option Values Effect
side "top" (default), "bottom", "left", "right" Which edge of the cavity to attach to
fill "none", "x", "y", "both" Stretch the widget to fill its parcel
expand True / False Claim any extra space in the parent
anchor "n", "ne", "e", "se", "s", "sw", "w", "nw", "center" Where the widget sits inside a parcel bigger than itself
padx, pady pixels, or (left, right) / (top, bottom) tuples Space outside the widget
ipadx, ipady pixels Space inside the widget (makes it bigger)
before, after a widget Insert this widget into the packing order relative to another
in_ a widget Pack into a different parent (rare)

fill and expand confuse people because they are different questions:

  • expand=True asks: give my parcel a share of the spare space. It enlarges the parcel, not the widget.
  • fill asks: make the widget use all of its parcel in the chosen direction.

A button with only expand=True gets a bigger parcel but stays small in the middle of it. A button with only fill="x" stretches across the parcel's width but gets no extra height. To grow with the window you need both: pack(fill="both", expand=True), the standard recipe for the main content area, a Text or a Canvas.

Common layouts

A vertical form:

Python
for text in ("Name", "Email", "City"):
    tk.Label(root, text=text, anchor="w").pack(fill="x", padx=12, pady=(8, 0))
    tk.Entry(root).pack(fill="x", padx=12)

A row of buttons. Use side="left" (they line up from the left) or side="right" (the first one ends up rightmost):

Python
bar = tk.Frame(root)
bar.pack(fill="x", padx=12, pady=12)
tk.Button(bar, text="Cancel").pack(side="right")
tk.Button(bar, text="OK").pack(side="right", padx=(0, 8))

A classic app skeleton: toolbar on top, status bar at the bottom, content filling the middle. Pack the edges first and the expanding widget last:

Python
toolbar = tk.Frame(root, bg="#dee2e6", height=36)
toolbar.pack(side="top", fill="x")
status = tk.Label(root, text="Ready", anchor="w", bd=1, relief="sunken")
status.pack(side="bottom", fill="x")
editor = tk.Text(root)
editor.pack(side="top", fill="both", expand=True)

If the editor were packed before the status bar, a shrinking window would squeeze the status bar out of sight, because the editor would already have claimed the space. When part of your layout vanishes, suspect packing order.

Equal-width buttons: fill="x" inside a vertical stack makes all buttons as wide as the column, and ipadx adds interior breathing room.

Padding in two places

padx/pady add outer margin: space between widgets. ipadx/ipady increase the widget's own size. A tuple lets you pad one side only: pady=(20, 0) adds 20 pixels above and none below. The model makes it concrete:

Python Stand-in: outer versus inner padding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Managing packed widgets

Python
button.pack_forget()          # hide, remembering nothing
button.pack()                 # show again (at the END of the order)
button.pack_info()            # dict of current options
root.pack_slaves()            # list of packed children, in order
button.pack_configure(fill="x")   # change options later

A hidden widget that is re-packed goes to the end of the packing order. To keep its position, pack it with before= or after=, or use grid and grid_remove instead.

When pack gets awkward

pack is great for one-direction flow. It struggles with tables, forms with aligned columns and anything needing both horizontal and vertical alignment. Instead of nesting a dozen frames, use the grid manager. Remember the one hard rule: never mix pack and grid in the same parent. Tk will detect the conflict and may hang trying to resolve it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do you need for a Text widget to grow in both directions when the window is resized?

    expand gives the parcel the extra space, and fill makes the widget use all of it.
  2. 2. A status bar at the bottom disappears when the window is made small. What is a likely cause?

    Pack in the order edges first, expanding content last, so edge widgets keep their space.
  3. 3. What is the difference between padx and ipadx?

    External padding grows the parcel; internal padding grows the widget itself.
  4. 4. Which is NOT allowed in one parent?

    A parent can use only one manager for its direct children. A child frame may use a different one for its own children.

Summary

  • pack carves parcels from the remaining cavity of the parent, in call order.
  • side picks the edge; fill stretches the widget; expand claims spare space; anchor positions inside the parcel.
  • padx/pady are outside margins; ipadx/ipady enlarge the widget; tuples pad one side.
  • For app skeletons, pack edge widgets first and the fill="both", expand=True widget last.
  • Do not mix pack and grid in the same parent; use frames to separate them.

Next: The grid Layout Manager