Tkinter Designer

← tkinter how-to

How do I make a table in tkinter?

There is no Table widget in tkinter. The table is ttk.Treeview: name the columns, give each a heading, and pass show="headings" so it does not add an empty tree column on the left. Each row is one insert call:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

tree = ttk.Treeview(root, columns=("name", "age"), show="headings")
tree.heading("name", text="Name")
tree.heading("age", text="Age")
tree.column("age", width=60, anchor="center")
tree.pack(fill="both", expand=True)

for row in [("Ana", 31), ("Ben", 7), ("Cy", 100)]:
    tree.insert("", "end", values=row)

root.mainloop()

The empty column on the left

A Treeview is built to show a tree, and by default it shows both the tree column (#0, 200 pixels wide) and your headings. As a table that is just a blank strip before the first real column. show="headings" hides it. The column names you pass are identifiers for your code; the text people see is set with heading().

Reading the selected row

tree.selection() returns item ids like I001, not data. Ask for the row with tree.item(iid, "values"). The surprise is that everything comes back as a string: insert 31 and you read back "31", so convert before doing arithmetic. Bind <<TreeviewSelect>> to react when the selection changes.

def on_select(event):
    for iid in tree.selection():
        name, age = tree.item(iid, "values")
        print(name, int(age))    # values come back as strings

tree.bind("<<TreeviewSelect>>", on_select)

Clearing it and loading rows from a database

tree.delete(*tree.get_children()) empties the table in one call. Rows from sqlite3 are tuples already, so they go straight into values=. The same string rule applies on the way out: an INTEGER column read back from the table is text.

tree.delete(*tree.get_children())
for row in conn.execute("SELECT name, age FROM people"):
    tree.insert("", "end", values=row)

Sorting when a heading is clicked

Treeview does not sort itself. Give the heading a command, read the column, sort it and move each row into its new place. Because the values are strings, a plain sort puts 100 before 25 and 7. Sort numeric columns as numbers.

def sort_by(col, numeric=False):
    rows = [(tree.set(iid, col), iid) for iid in tree.get_children()]
    rows.sort(key=lambda r: float(r[0]) if numeric else r[0].lower())
    for index, (_, iid) in enumerate(rows):
        tree.move(iid, "", index)

tree.heading("age", text="Age", command=lambda: sort_by("age", numeric=True))

Scrollbars and striped rows

A long table needs a scrollbar wired both ways, the same as a Listbox: the scrollbar's command drives tree.yview, and the tree's yscrollcommand moves the scrollbar. For stripes, give alternate rows a tag and colour the tag.

scrollbar = ttk.Scrollbar(root, orient="vertical", command=tree.yview)
tree.configure(yscrollcommand=scrollbar.set)

for i, row in enumerate(rows):
    tree.insert("", "end", values=row, tags=("odd" if i % 2 else "even",))
tree.tag_configure("odd", background="#f0f4f8")

In CustomTkinter and ttkbootstrap

CustomTkinter has no table widget. A ttk.Treeview works inside a CustomTkinter window, but CustomTkinter does not restyle ttk widgets, so it keeps the system look in dark mode. The separate CTkTable package (pip install CTkTable, written by a different author) builds a table out of CustomTkinter widgets instead.

ttkbootstrap ships Tableview, a Treeview with a search box and paging built in. Import it from ttkbootstrap.widgets.tableview, which works on 1.20 and 2.x.

import ttkbootstrap as ttkb
from ttkbootstrap.widgets.tableview import Tableview

app = ttkb.Window()
table = Tableview(
    app,
    coldata=["Name", "Age"],
    rowdata=[("Ana", 31), ("Ben", 7), ("Cy", 100)],
    searchable=True,
    paginated=True,
)
table.pack(fill="both", expand=True)
app.mainloop()

Doing it without the lookup

Draw a Treeview and type the column headings into the inspector. The export sets show="headings", writes one heading() per column, and adds a load_<name>(rows) method that clears the table and inserts whatever rows you pass it, so filling it from a database is a single call.

This example is ready to open: change it on the canvas and watch the Python update.

Open this example in the designer →

Related questions