Is there a way to sort the entries in a Tk Treeview by clicking the column? Surprisingly, I could not find any documentation/tutorial for this.
+4
A:
patthoyts from #tcl
pointed out that the TreeView Tk demo program had the sort functionality. Here's the Python equivalent of it:
def treeview_sort_column(tv, col, reverse):
l = [(tv.set(k, col), k) for k in tv.get_children('')]
l.sort(reverse=reverse)
# 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: \
treeview_sort_column(tv, col, not reverse))
[...]
columns = ('name', 'age')
treeview = ttk.TreeView(root, columns=columns, show='headings')
for col in columns:
treeview.heading(col, text=col, command=lambda: \
treeview_sort_column(tv, col, False))
[...]
Sridhar Ratnakumar
2009-12-28 02:49:20
+1
A:
Along with #tcl, other apt resources for Tkinter research are the Tkinter mailing list and Wiki.
Cameron Laird
2009-12-28 16:13:56
Welcome to Stackoverflow. :-) Ideally, you should post this as a *comment* (to my answer), but not as an answer itself.
Sridhar Ratnakumar
2009-12-29 01:38:10
Oh, and thanks for the pointer to `Tkinter-discuss` list which is new to me.
Sridhar Ratnakumar
2009-12-29 01:39:32