python Tk 树视图列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1966929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:27:31  来源:igfitidea点击:

Tk treeview column sort

pythonsortingtreeviewtkinter

提问by Sridhar Ratnakumar

Is there a way to sort the entries in a Tk Treeviewby clicking the column? Surprisingly, I could not find any documentation/tutorial for this.

有没有办法通过单击列来对Tk 树视图中的条目进行排序?令人惊讶的是,我找不到任何文档/教程。

回答by Sridhar Ratnakumar

patthoytsfrom #tclpointed out that the TreeView Tk demo program had the sort functionality. Here's the Python equivalent of it:

patthoytsfrom#tcl指出 TreeView Tk 演示程序具有排序功能。这是它的 Python 等价物:

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))
[...]

回答by madonius

This did not work in python3. Since the Variable was passed by reference, all lambdas ended up refering to the same, last, element in columns.

这在python3中不起作用。由于变量是通过引用传递的,因此所有 lambda 表达式最终都引用了列中相同的最后一个元素。

This did the trick for me:

这对我有用:

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

回答by Federico Dorato

madoniusis right, but here you have the full example and a proper, understandable explanation

madonius是对的,但这里有完整的示例和适当的、易于理解的解释

The answer provided by Sridhar Ratnakumardoes not work in python3 (and apparently in python2.7): since the variable is passed by reference, all lambdas end up referring to the same, last, element in columns.

Sridhar Ratnakumar提供的答案在 python3 中不起作用(显然在 python2.7 中):由于变量是通过引用传递的,因此所有 lambda 最终都引用列中相同的最后一个元素。

You just need to change this for loop:

你只需要改变这个for loop

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

And the same change has to be applied to the lambda function inside treeview_sort_column

并且必须将相同的更改应用于 treeview_sort_column 中的 lambda 函数

So the complete solution would look like this:

所以完整的解决方案如下所示:

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, text=col, command=lambda _col=col: \
                 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 _col=col: \
                     treeview_sort_column(treeview, _col, False))
[...]