Skip to main content
 首页 » 编程设计

python之无法在 Gtk.TextView i Python 3 中对列进行排序

2024年12月31日752php

当我尝试让 Python 中的 Gtk.TreeView 可以按列排序时,我使用类似的东西

for i, column_title in enumerate(["Title A", "Title B"]): 
    renderer = Gtk.CellRendererText() 
    column = Gtk.TreeViewColumn(column_title, renderer, text=i) 
    column.set_sort_indicator(True) 
    column.set_sort_order(Gtk.SortType.ASCENDING) 
    column.set_sort_column_id(i)  # trying to make columns sortable 
    self.study_view.append_column(column) 

但是当我点击列的标题时,我收到了错误消息

(main.py:14540): Gtk-CRITICAL **: 22:46:43.573: gtk_tree_sortable_get_sort_column_id: assertion 'GTK_IS_TREE_SORTABLE (sortable)' failed 

我用谷歌搜索了这个问题,但没有找到解决方案。文档没有给出任何提示。怎么了?

这是完整的 Python 应用程序中给出错误消息的行:

https://github.com/nordlow/dicom/blob/master/main.py#L241

我正在使用这些文档作为引用:

https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html?highlight=view#sorting

请您参考如下方法:

这是 Gtk 中最复杂的机器之一。您需要用排序后的模型包裹过滤后的TreeModel,以便您可以对过滤后的模型再次进行排序。请注意,在使用过滤模型包装之前,普通 TreeModel 是可排序的。所以你的代码在 225 行应该看起来像这样:

self.study_store = StudyStore(client=self.client) 
self.current_study_filter = None 
self.study_filter = self.study_store.filter_new() 
self.study_filter.set_visible_func(self.study_view_filter_func) 
self.study_sort = Gtk.TreeModelSort(self.study_filter) 
self.study_view = Gtk.TreeView.new_with_model(self.study_sort) 

附言我认为你过度使用了类变量。请不要冒犯。