【问题标题】:TTK Treeview not sorting ID numericallyTTK Treeview 未对 ID 进行数字排序
【发布时间】:2017-11-26 11:01:15
【问题描述】:

我最近发现了一种用于树视图的排序解决方案,它允许我按字母顺序对表中的每条记录进行排序,但是当我尝试按 ID 排序时,它会在 1-20 中加载,它不会重新排序到 1-20。我的代码和屏幕截图如下所示。该代码对所有其他列进行了正确排序。

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))
for col in tv['columns']:
tv.heading(col, text=col, command=lambda _col=col:
           treeview_sort_column(tv, _col, False))

以上不是我的代码,因此我需要帮助。

截图链接:https://imgur.com/a/EVq2d

编辑:显示完整代码的链接:https://imgur.com/a/KYJM0

谢谢, 乔什。

【问题讨论】:

  • 你确定你有数字,而不是数字“1”-“20”的字符串吗?
  • 表中的值来自 sqlite3 查询,并且有一行代码将其存储的变量显式更改为整数但没有更改:(
  • 它的排序就像你有字符串而不是数字。
  • 永远不要将代码和错误消息作为屏幕截图 - 我们不能复制它以用于回答或评论。
  • 顺便说一句:您不能使用一个SELECT * FROM ... 获取行中的所有数据吗?如果你有i,你为什么要使用song_int - 你可以使用for song_int in range(20))

标签: python-3.x sorting tkinter treeview ttk


【解决方案1】:

对于那些寻找正确答案的人,它在这个线程上:

python ttk treeview sort numbers

【讨论】:

    猜你喜欢
    • 2014-03-28
    • 2018-01-16
    • 2013-03-26
    • 1970-01-01
    • 2020-08-05
    • 2015-12-21
    • 2013-12-03
    • 2011-12-14
    相关资源
    最近更新 更多