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
| Widget | Purpose |
|---|---|
Label | Static text or an image |
Button | Clickable action |
Entry | One line of typed text |
Text | Multi-line editable text |
Checkbutton | On/off toggle |
Radiobutton | One choice from a group |
Combobox | Drop-down list (from tkinter.ttk) |
Frame | An invisible container for grouping other widgets |
Listbox | A 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
| Manager | Model | Use when |
|---|---|---|
.pack() | Stack widgets against a side (top, left, ...) | Simple vertical or horizontal strips |
.grid() | Place widgets in rows and columns | Forms, anything table-shaped — the default choice |
.place() | Absolute x/y pixel coordinates | Almost 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
- Mixing
packandgridin 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. - Chaining the layout call onto the constructor:
entry = ttk.Entry(root).pack()setsentrytoNone, becausepack()returns nothing. Create the widget on one line, lay it out on the next. - Forgetting
weight: agridrow or column with noweightnever grows, no matter how muchstickyyou add. - Using
place()for a real layout: absolute pixels look right on your machine and wrong on every other DPI, font size, and window size. - Reading an
Entrybeforemainloop():username.get()written at the bottom of the script runs before the user has typed anything. Read values inside a callback. - 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 thanpackand degrades more gracefully. - Use
ttkwidgets: native theming for free, and a real styling system viattk.Style(). - Set
paddingon frames, not margins on every widget: onepadding=16beats twentypadxarguments. - Name your rows and columns with constants in bigger forms —
ROW_EMAIL = 2is 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
- The Contact Form: Build a form with Name, Email, and a multi-line Message, plus Submit and Clear buttons. Use
gridand make the input column stretch when the window widens. - The Calculator Face: Lay out a 4x4 grid of number and operator buttons with a display
Entryspanning the top row. Every button should be the same size, and the whole keypad should scale with the window. - 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.