python - 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))
解决方案
谢谢(刚学会)它确实解决了这个问题。它立即简单地解决了我所有的问题……比我尝试过的所有方法都容易得多!
对于那些寻找解决方案的人
l.sort(key=lambda t: int(t[0]), reverse=reverse)
推荐阅读
- jquery - 使用 Jquery 进行动态搜索
- scipy - 对对象使用 scipy 层次聚类
- android - 不更新目标NDK平台有什么好处吗?
- python - Beautifulsoup python 类解析
- vim - Vim Fugitive:切换“:Gblame”功能
- c# - WPF:DoubleAnimation“到”属性绑定时间
- javascript - 更改组件的路由
- sql-server - SQL Server Migration Assistant for Access 是否保留表关系?
- php - 如何更改父类函数中受保护变量的值
- amazon-ec2 - 自动为 Spot 实例分配 IP 地址