无法在Python 3中的Gtk.TextView中对列进行排序

时间:2018-10-16 20:55:43

标签: python python-3.x gtk3

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

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

1 个答案:

答案 0 :(得分:1)

这是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)

P.S。我认为您正在过度使用类变量。请不要冒犯。