【发布时间】: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