【问题标题】:Treeview Tuple Sort as String (Wrong Order)Treeview 元组排序为字符串(错误顺序)
【发布时间】:2020-04-02 13:35:15
【问题描述】:

好吧,经过几个小时的摆弄,我似乎无法修复这个错误。 我尝试使用 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))

【问题讨论】:

  • 你的输出应该是什么样子的?
  • 它应该在元组中将较小的数字(9234 和 9250)放在较大的数字(27958)之前。 [('9234', 'I00D'), ('9250', 'I00E'), ('27958', 'I008'), ('28497', 'I00C'), ('28652', 'I018') , ('28653', 'I001'), ('28713', 'I009'), ('29262', 'I00A'), ('29448', 'I00B')]

标签: python python-3.x tkinter treeview


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多