tkinter and CustomTkinter · lesson 3 of 8
Parts in this lesson
A.grid(row=0, column=1)rows and columns
Bsticky="ew"stretch to fill the cell
Ccolumnconfigure(1, weight=1)which column grows
Dbuttons = ttk.Frame(self)4.15: composition

Layout with grid

Last updated

pack puts widgets one under another, which suits a label and a button. A form needs rows and columns: a label on the left, a box on the right, and the boxes growing when the window does. In this lesson you'll lay out a form with grid, put frames inside frames, and find out why one frame can't use both.

By the end of this lesson you can

  • Lay out widgets in rows and columns with grid, sticky and padding
  • Make a column grow with the window using columnconfigure
  • Put a frame inside a frame, each with its own layout
  • Read the TclError you get from mixing pack and grid

Rows and columns

grid places each widget in a cell, by row and column, counting from 0 like a list. Here's a form to lend an item. Save it as form.py and run it:

import tkinter as tk
from tkinter import ttk


class LendForm(ttk.Frame):
    def __init__(self, root: tk.Misc) -> None:
        super().__init__(root, padding=12)
        ttk.Label(self, text="Item id").grid(row=0, column=0, sticky="w")
        ttk.Entry(self).grid(row=0, column=1, sticky="ew", padx=(8, 0), pady=4)
        ttk.Label(self, text="Member id").grid(row=1, column=0, sticky="w")
        ttk.Entry(self).grid(row=1, column=1, sticky="ew", padx=(8, 0), pady=4)
        buttons = ttk.Frame(self)
        buttons.grid(row=2, column=1, sticky="e", pady=(8, 0))
        ttk.Button(buttons, text="Lend").pack(side="left")
        ttk.Button(buttons, text="Return").pack(side="left", padx=(8, 0))
        self.columnconfigure(1, weight=1)


root = tk.Tk()
root.title("Lend an item")
LendForm(root).grid(row=0, column=0, sticky="nsew")
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
root.mainloop()
A window titled Lend an item, with Item id and Member id labels beside entry boxes, and Lend and Return buttons at the bottom right
form.py on Windows 11.

The cells look like this:

The grid
            column 0      column 1
row 0       Item id       [ entry........... ]
row 1       Member id     [ entry........... ]
row 2                          [Lend] [Return]
  • row= and column= choose the cell. A column is as wide as its widest widget.
  • sticky says which sides of the cell a widget sticks to, by compass point: "w" is west, the left side; "ew" is east and west, so the widget stretches across the cell; "nsew" fills it.
  • padx and pady add space, as with pack. (8, 0) is 8 pixels on the left and none on the right.

Growing with the window

Drag a corner of the window to make it wider. The entries grow, and the labels don't. That's this line:

self.columnconfigure(1, weight=1)

When there's spare room, grid shares it out by weight. Column 1 has weight 1, and column 0 has the default, 0, so column 1 gets all of it. The entries are sticky="ew", so they stretch with their column.

The last three lines do the same one level up: the form sits in the window's only cell, sticky="nsew", and that row and column have weight 1, so the form fills the window.

Try a change: take columnconfigure away and make the window wider. The form stays the same size, in the corner.

Two columns both have weight=1. The window gets 100 pixels wider. What happens?

Frames inside frames

The two buttons sit side by side in one cell. That's because they aren't in the form's grid at all: they're in a small frame of their own, buttons, and that frame sits in row 2, column 1.

Inside buttons, they're placed with pack(side="left"), which puts them in a row. Each frame has its own layout, so a frame laid out with grid can hold a frame laid out with pack.

This is composition (lesson 4.15): the form has a button bar, which has two buttons. Bigger windows are built the same way, out of frames that each look after one part, and each part can be its own class.

One frame, two layouts

The wrong way. Why the extra frame? Try placing the buttons straight into the form with pack, next to the labels placed with grid:

import tkinter as tk
from tkinter import ttk


class LendForm(ttk.Frame):
    def __init__(self, root: tk.Misc) -> None:
        super().__init__(root, padding=12)
        ttk.Label(self, text="Item id").grid(row=0, column=0, sticky="w")
        ttk.Entry(self).grid(row=0, column=1, sticky="ew", padx=(8, 0), pady=4)
        ttk.Label(self, text="Member id").grid(row=1, column=0, sticky="w")
        ttk.Entry(self).grid(row=1, column=1, sticky="ew", padx=(8, 0), pady=4)
        ttk.Button(self, text="Lend").pack(side="left")
        ttk.Button(self, text="Return").pack(side="left", padx=(8, 0))


root = tk.Tk()
root.title("Lend an item")
LendForm(root).grid(row=0, column=0, sticky="nsew")
root.mainloop()
Terminal
python form.py
Traceback (most recent call last):
  File "form.py", line 18, in <module>
    LendForm(root).grid(row=0, column=0, sticky="nsew")
  File "form.py", line 12, in __init__
    ttk.Button(self, text="Lend").pack(side="left")
  ... (lines inside tkinter)
_tkinter.TclError: cannot use geometry manager pack inside .!lendform which already has slaves managed by grid

Read the last line. A geometry manager is what places widgets: pack or grid. .!lendform is Tk's own name for the form, and slaves is the old Tk word for the widgets a manager looks after. (Newer versions of Tk say content instead.) So: the form's widgets are already placed by grid, and pack can't place others in the same frame. Two managers would fight over the same space.

The fix is the one from step 3: the buttons go in their own frame, and the form places that frame with grid.

Errors that come from Tk itself, not from Python, are TclErrors. Tcl is the language Tk is written for, and tkinter sends it commands behind the scenes.

Check

What does sticky="ew" do?
A frame's labels are placed with grid. How do you put two buttons side by side using pack?
Where does a TclError come from?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python