Widgets & Layout Managers: Arranging the Interface

Widgets & Layout Managers: Arranging the Interface in Python: A window is an empty box. Widgets are the things you put in it — buttons, labels, text fields,…

Concept Explanation

A window is an empty box. Widgets are the things you put in it — buttons, labels, text fields, checkboxes. A layout manager decides where each widget lands and what happens to it when the user drags the window bigger.

Beginners spend hours fighting the second half of that sentence. The fix is understanding that Tkinter gives you three layout managers, they solve different problems, and you must never mix two of them inside the same container.

The Widget Vocabulary

WidgetPurpose
LabelStatic text or an image
ButtonClickable action
EntryOne line of typed text
TextMulti-line editable text
CheckbuttonOn/off toggle
RadiobuttonOne choice from a group
ComboboxDrop-down list (from tkinter.ttk)
FrameAn invisible container for grouping other widgets
ListboxA scrollable list of options

Always prefer the ttk versions (ttk.Button, ttk.Entry) over the plain ones. They use the operating system's native styling, so your app looks like it belongs on the machine rather than like it was built in 1995.

The Three Layout Managers

ManagerModelUse when
.pack()Stack widgets against a side (top, left, ...)Simple vertical or horizontal strips
.grid()Place widgets in rows and columnsForms, anything table-shaped — the default choice
.place()Absolute x/y pixel coordinatesAlmost never; it breaks on resize and on other people's screens

Frames: The Key to Complex Layouts

The "never mix managers" rule is per-container, not per-window. So the professional pattern is: split the window into Frames, then pick whatever manager suits inside each frame.

sidebar = ttk.Frame(root)
sidebar.pack(side="left", fill="y")       # frame packed into the window

ttk.Button(sidebar, text="Open").grid(row=0, column=0)   # grid INSIDE the frame
ttk.Button(sidebar, text="Save").grid(row=1, column=0)

Making Things Actually Resize

Two arguments do most of the work:

  • fill — "x", "y", or "both": should the widget stretch to fill the space it was given?
  • expand=True — should the widget be given extra space when the window grows?

In grid, the equivalent is sticky="nsew" on the widget plus columnconfigure(index, weight=1) on the parent. A grid cell will not stretch unless its row or column has a non-zero weight. This single fact resolves the majority of "my layout doesn't resize" questions.


Code Example #1 (Basic)

A login form built with grid — the shape you will reuse constantly.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Sign In")
root.geometry("320x160")

form = ttk.Frame(root, padding=16)
form.pack(fill="both", expand=True)

ttk.Label(form, text="Username:").grid(row=0, column=0, sticky="w", pady=4)
username = ttk.Entry(form)
username.grid(row=0, column=1, sticky="ew", pady=4)

ttk.Label(form, text="Password:").grid(row=1, column=0, sticky="w", pady=4)
password = ttk.Entry(form, show="*")          # masks the characters
password.grid(row=1, column=1, sticky="ew", pady=4)

remember = tk.BooleanVar(value=True)
ttk.Checkbutton(form, text="Remember me", variable=remember).grid(
    row=2, column=1, sticky="w", pady=4
)

ttk.Button(form, text="Sign In").grid(row=3, column=1, sticky="e", pady=(12, 0))

# Column 1 absorbs any extra width; column 0 stays label-sized
form.columnconfigure(1, weight=1)

root.mainloop()

Drag the window wider and the entry fields grow while the labels stay put. That is weight=1 plus sticky="ew" doing their job.

Code Example #2 (Intermediate)

A two-pane layout using nested frames, each with its own manager.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Notes")
root.geometry("640x400")
root.minsize(400, 250)                 # stop users shrinking it into nonsense

# --- Left: a fixed-width sidebar, packed ---
sidebar = ttk.Frame(root, padding=8, width=150)
sidebar.pack(side="left", fill="y")
sidebar.pack_propagate(False)          # honour width= instead of shrinking to fit

ttk.Label(sidebar, text="Notebooks", font=("", 10, "bold")).pack(anchor="w")
for name in ("Work", "Personal", "Ideas", "Archive"):
    ttk.Button(sidebar, text=name).pack(fill="x", pady=2)

# --- Right: the editor, gridded ---
editor = ttk.Frame(root, padding=8)
editor.pack(side="right", fill="both", expand=True)

ttk.Label(editor, text="Title:").grid(row=0, column=0, sticky="w")
ttk.Entry(editor).grid(row=0, column=1, sticky="ew", padx=(6, 0))

body = tk.Text(editor, wrap="word", height=10)
body.grid(row=1, column=0, columnspan=2, sticky="nsew", pady=(8, 0))

scroll = ttk.Scrollbar(editor, orient="vertical", command=body.yview)
scroll.grid(row=1, column=2, sticky="ns", pady=(8, 0))
body.configure(yscrollcommand=scroll.set)

# The text area is the part that should grow
editor.columnconfigure(1, weight=1)
editor.rowconfigure(1, weight=1)

root.mainloop()

The window uses pack. Each frame internally uses grid. No container mixes the two — and the whole thing resizes correctly.


Common Mistakes

  1. Mixing pack and grid in one container: Tkinter does not raise an error. It freezes, showing an empty or half-drawn window while the two managers deadlock. Use frames to separate them.
  2. Chaining the layout call onto the constructor: entry = ttk.Entry(root).pack() sets entry to None, because pack() returns nothing. Create the widget on one line, lay it out on the next.
  3. Forgetting weight: a grid row or column with no weight never grows, no matter how much sticky you add.
  4. Using place() for a real layout: absolute pixels look right on your machine and wrong on every other DPI, font size, and window size.
  5. Reading an Entry before mainloop(): username.get() written at the bottom of the script runs before the user has typed anything. Read values inside a callback.
  6. Forgetting the parent argument: ttk.Button(text="Hi") with no parent attaches to the root window by surprise rather than to the frame you meant.

Best Practices

  • Default to grid: it handles forms, tables, and mixed alignment better than pack and degrades more gracefully.
  • Use ttk widgets: native theming for free, and a real styling system via ttk.Style().
  • Set padding on frames, not margins on every widget: one padding=16 beats twenty padx arguments.
  • Name your rows and columns with constants in bigger forms — ROW_EMAIL = 2 is far easier to reshuffle than a scatter of magic numbers.
  • Call minsize(): it prevents users from dragging your window into an unusable sliver.
  • Group with frames early: retrofitting frames into a flat 40-widget window is painful; starting with them costs nothing.

Practice Exercises

  1. The Contact Form: Build a form with Name, Email, and a multi-line Message, plus Submit and Clear buttons. Use grid and make the input column stretch when the window widens.
  2. The Calculator Face: Lay out a 4x4 grid of number and operator buttons with a display Entry spanning the top row. Every button should be the same size, and the whole keypad should scale with the window.
  3. The Three-Pane App: Using nested frames, build a window with a top toolbar, a left sidebar, and a main content area. Only the content area should grow when the window is resized — verify by dragging each edge.