Tkinter Designer

← tkinter how-to

How do I add a separator line in tkinter?

Use ttk.Separator with an orient, and give it a length when you place it. On its own a separator has no size, which is why the usual first attempt appears to do nothing at all.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tk.Label(root, text="Account").pack()
ttk.Separator(root, orient="horizontal").pack(fill="x", pady=8)
tk.Label(root, text="Payment").pack()

root.mainloop()

Why yours is invisible

A separator asks for no space of its own: packed without fill, a horizontal one measures one pixel by one pixel, which is a dot you will never find on a 300-pixel window. Adding fill="x" stretches it across its parent and it appears immediately, at one pixel tall and as wide as the space allows.

The same applies to a vertical separator, which needs fill="y" to have any height. In a grid, sticky="ew" and sticky="ns" do the same job, and a horizontal rule usually wants columnspan so it crosses the whole layout rather than one column.

ttk.Separator(root, orient="horizontal").pack(fill="x")
ttk.Separator(root, orient="vertical").pack(side="left", fill="y")

# in a grid
ttk.Separator(root, orient="horizontal").grid(row=2, columnspan=3, sticky="ew")

Giving it breathing room

A divider works by the space around it as much as the line itself, so pad it rather than butting it against the widgets on either side. pady on a horizontal separator and padx on a vertical one is usually enough to make a window read as sections rather than a list.

When a separator is the wrong answer

If the widgets either side belong together and the line is there to say so, a LabelFrame states the grouping outright and gives it a caption. Reach for a separator when you want a break with no name attached to it.

Doing it without the lookup

Drag a Separator on and give it a width on the canvas — because the designer places widgets absolutely, it already has a length, so the invisible-separator problem cannot happen. The orientation is a dropdown in the inspector.

Lay the window out visually and read the generated Python as you go.

Open the tkinter designer →

Related questions