Tkinter TreeView – Enable Sorting upon Clicking Column Headings

The below code snippet can help you achieve the Sorting Option on Treeview Column Headings in Tkinter in both ascending and descending order.

columns = ("Student","Course","Last Session")

tv = Treeview(popup, show='headings', columns=columns,height=25)

def treeview_sort_column(tv, col, reverse):
    l = [(tv.set(k, col), k) for k in tv.get_children('')]

    # rearrange items in sorted positions
    for index, (val, k) in enumerate(l):
        tv.move(k, '', index)

    # reverse sort next time
    tv.heading(col, command=lambda _col=col: treeview_sort_column(tv, _col, not reverse))

for col in columns:
    tv.heading(col, text=col,command=lambda _col=col: treeview_sort_column(tv, _col, False))



ModuleNotFoundError: No module named ‘babel.numbers’

Traceback (most recent call last):
File "tkinter/", line 1705, in __call__
File "", line 726, in
File "", line 536, in calendar
File "tkcalendar/", line 211, in __init__
File "babel/", line 314, in get_day_names
File "babel/", line 641, in days
File "babel/", line 364, in _data
File "babel/", line 111, in load
File "babel/", line 111, in load
File "babel/", line 117, in load
ModuleNotFoundError: No module named 'babel.numbers'

Solution Explained

Locate the tkcalendar folder under venv/lib/python3.x/site-packages of your project folder.

Under, add an additional import for the missing module.

import calendar
from babel.dates import format_date, parse_date, get_day_names, get_month_names
from babel.numbers import *  # Additional Import