是否可以通过单击列对Tk树视图中的条目进行排序?令人惊讶的是,我找不到任何文档/教程。
patthoyts从中#tcl指出,TreeView Tk演示程序具有排序功能。这是等效的Python:
#tcl
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(treeview, col, False)) [...]