带有浮点数的树视图排序列
treeview sort column with float number
我尝试通过点击标题对列进行排序,但结果出错。
我想按名称和编号重新排序
def treeview_sort_column(product_table, col, reverse):
l = [(product_table.set(k, col), k) for k in product_table.get_children('')]
l.sort(key=lambda t: float(t[0]), reverse=reverse)
for index, (val, k) in enumerate(l):
product_table.move(k, '', index)
product_table.heading(col, command=lambda _col=col: treeview_sort_column(product_table, _col, not reverse))
当我尝试重新排序时显示错误。
ValueError: could not convert string to float: '3,046.00'
单击列 header 以重新排序名称和编号的完整功能。
def treeview_sort_column(product_table, col, reverse):
l = [(product_table.set(k, col), k) for k in product_table.get_children('')]
try:
l.sort(key=lambda t: float(t[0].replace(",","")), reverse=reverse)
except:
l.sort(reverse=reverse)
for index, (val, k) in enumerate(l):
product_table.move(k, '', index)
product_table.heading(col, command=lambda _col=col: treeview_sort_column(product_table, _col, not reverse))
for col in header:
product_table.heading(col, text=col,command=lambda _col=col: treeview_sort_column(product_table, _col, False))
我尝试通过点击标题对列进行排序,但结果出错。
我想按名称和编号重新排序
def treeview_sort_column(product_table, col, reverse):
l = [(product_table.set(k, col), k) for k in product_table.get_children('')]
l.sort(key=lambda t: float(t[0]), reverse=reverse)
for index, (val, k) in enumerate(l):
product_table.move(k, '', index)
product_table.heading(col, command=lambda _col=col: treeview_sort_column(product_table, _col, not reverse))
当我尝试重新排序时显示错误。
ValueError: could not convert string to float: '3,046.00'
单击列 header 以重新排序名称和编号的完整功能。
def treeview_sort_column(product_table, col, reverse):
l = [(product_table.set(k, col), k) for k in product_table.get_children('')]
try:
l.sort(key=lambda t: float(t[0].replace(",","")), reverse=reverse)
except:
l.sort(reverse=reverse)
for index, (val, k) in enumerate(l):
product_table.move(k, '', index)
product_table.heading(col, command=lambda _col=col: treeview_sort_column(product_table, _col, not reverse))
for col in header:
product_table.heading(col, text=col,command=lambda _col=col: treeview_sort_column(product_table, _col, False))