PythonGTK+3:通过单击列对树视图进行排序

2022-05-16 00:00:00 python gtk3

问题描述

我希望能够通过单击列来按列对树视图进行排序。我只是引用文档中的流行示例作为参考(https://python-gtk-3-tutorial.readthedocs.io/en/latest/treeview.html):

        self.treeview = Gtk.TreeView.new_with_model(self.filter)
        for i, column_title in enumerate(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"]):
            renderer = Gtk.CellRendererText()
            column = Gtk.TreeViewColumn(column_title, renderer, text=i)
            self.treeview.append_column(column)
            column.set_sort_column_id(i)

除非我收到错误:

(test.py:26081):gtk-Critical**:12:35:03.856:gtk_tree_sortable_get_sortable_Column_id:断言‘gtk_is_tree_sortable(Sortable)’失败

(test.py:26081):gtk-关键**:12:35:03.856:gtk_TREE_SOLTABLE_HAS_DEFAULT_SORT_FUNC:断言‘GTK_IS_TREE_SOLTABLE(SOLTABLE)’失败

(test.py:26081):gtk-Critical**:12:35:03.856:gtk_tree_sortable_set_ort_Column_id:断言‘gtk_is_tree_sortable(Sortable)’失败


解决方案

在this post的帮助下,我想出了一个方法。

编辑以下行:

self.treeview = Gtk.TreeView.new_with_model(self.filter)

self.treeview = Gtk.TreeView.new_with_model(Gtk.TreeModelSort(self.filter))

并且应该可以工作。

相关文章