首页 > 解决方案 > Treeview 元组排序为字符串(错误顺序)

问题描述

在此处输入图像描述

好吧,经过几个小时的摆弄,我似乎无法修复这个错误。我尝试使用 key = operator.itemgetter(0) 将我的类型更改为整数,并且我还尝试了其他修复,例如使用迭代实用程序:链式,functools --> 没有成功

它返回一个元组列表,例如: [('27958', 'I008'), ('28497', 'I00C'), ('28652', 'I018'), ('28653', 'I001'), ( '28713', 'I009'), ('29262', 'I00A'), ('29448', 'I00B'), ('9234', 'I00D'), ('9250', 'I00E')]
如您可以看到,以 9 开​​头的数字在末尾,我假设是因为无论如何它都将其视为字符串。

我正在使用经常提到的基本 Treeview_sort_column 函数,没什么特别的。

def treeview_sort_column(t1, col, reverse):
    l = [(t1.set(k, int(col)), k) for k in t1.get_children('')]
    l.sort(reverse=reverse) 
    print(l)

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

    t1.heading(col, command=lambda _col=col: treeview_sort_column(t1, _col, not reverse))

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

标签: pythonpython-3.xtkintertreeview

解决方案


谢谢(刚学会)它确实解决了这个问题。它立即简单地解决了我所有的问题……比我尝试过的所有方法都容易得多!

对于那些寻找解决方案的人

l.sort(key=lambda t: int(t[0]), reverse=reverse)


推荐阅读