Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 40 ยท Layout Managers

The Tkinter grid() Layout Manager

Build aligned forms and tables with Tkinter's grid manager: row and column, sticky, spans, padding, weights for resizing, uniform columns and grid helper methods.

grid arranges widgets in a table of rows and columns. It is the best choice for forms (labels in one column, inputs in the next), calculators, dashboards and anything where things must line up both horizontally and vertically. It is the manager most professional Tkinter code uses for the main layout, with pack for small rows inside frames.

The first grid

Python
import tkinter as tk

root = tk.Tk()
root.title("Login")

tk.Label(root, text="Username").grid(row=0, column=0, sticky="e", padx=8, pady=4)
tk.Entry(root).grid(row=0, column=1, padx=8, pady=4)
tk.Label(root, text="Password").grid(row=1, column=0, sticky="e", padx=8, pady=4)
tk.Entry(root, show="*").grid(row=1, column=1, padx=8, pady=4)
tk.Button(root, text="Sign in").grid(row=2, column=0, columnspan=2, pady=8)

root.mainloop()

Rows and columns are numbered from 0 and need not be consecutive; empty rows and columns take no space. The width of a column is the width of its widest widget and the height of a row is the height of its tallest. The grid itself has no size options: it just follows its contents.

Options for grid()

Option Meaning
row, column Cell position (default: row 0, column 0 or next free)
rowspan, columnspan How many rows or columns the widget covers
sticky Which sides of the cell the widget sticks to
padx, pady Outer padding (single number or (a, b) tuple)
ipadx, ipady Inner padding, making the widget itself bigger
in_ Place into another parent (rare)

sticky

A cell may be larger than its widget (because another widget in the same column is wider). sticky is a string of compass letters n, s, e, w that says which cell edges the widget touches:

  • sticky="" (default): centered in the cell, natural size.
  • sticky="w": left-aligned. sticky="e": right-aligned (typical for form labels).
  • sticky="ew": stretched to the full width of the cell.
  • sticky="nsew": fills the whole cell in both directions.

Combine sticky with weight (below) to make parts of a layout resize with the window.

Spans

columnspan=2 lets a widget cover two columns, as the Sign-in button above does so that it is centered under both. A spanning widget does not force its width onto a single column; Tk distributes the extra space across the columns it covers. The stand-in below models column widths, spans and extra-space weights so you can predict results.

Python Stand-in: how grid sizes columns

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.

The last three lines show what columnconfigure does, which is the next topic.

Weights: making the grid resize

By default a grid stays at its natural size even if the window grows; extra space is left empty. To make a row or column stretch, give it a weight:

Python
root.columnconfigure(1, weight=1)     # column 1 takes extra width
root.rowconfigure(2, weight=1)        # row 2 takes extra height

Weights are relative: with weight=1 and weight=2, the second column receives two thirds of the extra space. A weight of 0 (the default) means "keep natural size". The resize recipe has two steps, and forgetting either is the most common grid mistake:

  1. Give the row or column a weight.
  2. Give the widget sticky so it actually fills the stretched cell.
Python
import tkinter as tk

root = tk.Tk()
root.geometry("420x260")

tk.Label(root, text="Notes:").grid(row=0, column=0, sticky="nw", padx=8, pady=8)
editor = tk.Text(root)
editor.grid(row=0, column=1, sticky="nsew", padx=(0, 8), pady=8)

tk.Button(root, text="Save").grid(row=1, column=1, sticky="e", padx=8, pady=(0, 8))

root.columnconfigure(1, weight=1)    # editor column grows horizontally
root.rowconfigure(0, weight=1)       # editor row grows vertically
root.mainloop()

Only the Text grows, while the "Notes:" label column and the Save row keep their sizes. The configuration of the parent, not the child, decides this, so columnconfigure is called on root, not on the editor.

Other options of rowconfigure/columnconfigure: minsize (a floor in pixels), pad (extra pixels added to the largest widget) and uniform="group" (every row or column with the same group name gets the same size, in proportion to its weight):

Python
for column in range(3):
    root.columnconfigure(column, weight=1, uniform="buttons")   # three equal columns

uniform is the neat answer to "make these columns exactly the same width", such as a calculator keypad.

A calculator keypad

Python
import tkinter as tk

root = tk.Tk()
root.title("Keypad")
display = tk.Entry(root, justify="right", font=("Consolas", 20))
display.grid(row=0, column=0, columnspan=4, sticky="ew", padx=6, pady=6)

keys = ["7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"]
for index, key in enumerate(keys):
    row, column = divmod(index, 4)
    tk.Button(root, text=key, width=4, height=2).grid(row=row + 1, column=column, sticky="nsew", padx=2, pady=2)

for column in range(4):
    root.columnconfigure(column, weight=1, uniform="key")
for row in range(1, 5):
    root.rowconfigure(row, weight=1, uniform="key")
root.mainloop()

divmod(index, 4) converts a flat list position into (row, column). This is the standard way to place a list of items in a grid with a fixed number of columns:

Python Stand-in: flat list to grid cells

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.

Grid helper methods

Python
widget.grid_remove()            # hide, remembering the options (grid() restores them)
widget.grid_forget()            # hide and forget the options
widget.grid_info()              # dict of options
root.grid_size()                # (columns, rows) currently used
root.grid_slaves(row=0)         # widgets in a row
root.grid_columnconfigure(0, weight=1)   # long form of columnconfigure
frame.grid_propagate(False)     # keep a requested frame size
tk.Label(root).grid_configure(sticky="w")  # change options later

grid_remove() is usually better than grid_forget() for "show/hide" sections because the widget returns to the same cell. Common errors:

  • TclError: cannot use geometry manager grid inside . which already has slaves managed by pack. You mixed managers in one parent. Put the widgets in a Frame.
  • Calling .grid() on the creation line and storing the result: entry = tk.Entry(root).grid(...) stores None, because grid() returns None. Create first, then grid on a new line.
  • Everything squashed into the corner on resize. You forgot the weights.
  • Text, Canvas or Listbox does not resize. It needs sticky="nsew" plus a weight on its row and column.

Mixing grid and pack

The standard pattern uses grid for the main arrangement and small frames with pack for rows of buttons:

Python
buttons = tk.Frame(root)
buttons.grid(row=5, column=0, columnspan=2, sticky="e", pady=8)
tk.Button(buttons, text="Cancel").pack(side="left", padx=4)
tk.Button(buttons, text="OK").pack(side="left")

The frame is one grid child of root, and inside it pack is free to run. If you need a layout that is neither (overlays, fixed coordinates), see the place manager.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does sticky='ew' do?

    sticky lists the cell edges the widget touches. Touching both east and west means full width.
  2. 2. A Text widget is in grid cell (0, 0) with sticky='nsew' but does not grow when the window is enlarged. What is missing?

    Rows and columns only take extra space when they have a weight. grid does not use expand or fill.
  3. 3. Why is 'entry = tk.Entry(root).grid(row=0, column=0)' a bug?

    Geometry methods return None. Create the widget on one line and place it on the next.
  4. 4. What does columnconfigure(0, weight=1, uniform='k') on several columns do?

    uniform groups share one size proportion, which creates equal-width columns.

Summary

  • grid(row, column) places widgets in a table; row heights and column widths follow the content.
  • sticky (n, s, e, w combinations) positions or stretches a widget inside its cell; columnspan and rowspan cover several cells.
  • Resizing needs two things: weight on the row or column of the parent and sticky on the widget.
  • uniform makes equal columns; minsize and pad fine-tune; grid_remove() hides and remembers.
  • Never mix pack and grid in one parent; use frames to combine them.

Next: The place Layout Manager