ttk.treeview增加标题单击排序功能

ttk.treeview增加标题单击排序功能


  1. 排序函数
def treeview_sort_column(tv, col, reverse):
    l = [(tv.set(k, col), k) for k in tv.get_children('')]
    #i = cols.index(col)

    #l.sort(key=lambda t: t[i], reverse=reverse)
    l.sort(reverse=reverse)
    #      ^^^^^^^^^^^^^^^^^^^^^^^

    for index, (val, k) in enumerate(l):
        tv.move(k, '', index)

    tv.heading(col,command=lambda: treeview_sort_column(tv, col, not reverse))
  1. 实现
        for col in cols:
            i = cols.index(col)
            #self.mat_list.heading(col, text=tree_head[i])
            self.mat_list.heading(col, text=tree_head[i] , command=lambda _col=col: treeview_sort_column(self.mat_list, _col, False))

你可能感兴趣的:(tkinter)