Python Gtk3:获取Treeview中已过滤行的数量

时间:2017-07-15 13:36:12

标签: python-3.x gtk gtk3 gtktreeview

我希望在应用过滤器后获取Treeview中的行数。我在过滤行后尝试使用len(Gtk.ListStore)和Gtk.Liststore.iter_n_children(),但这两个函数都返回了没有过滤的行总数。

有什么办法可以获得过滤后返回的行数吗?

编辑:Rawing要求的我的程序片段:

    self.listfortreeview = Gtk.ListStore(str, str, str, str, str, str, str, str, str, str, str)
    for item in win.result:
        self.listfortreeview.append(list(item))

    self.current_ps_filter = None

    self.ps_filter = self.listfortreeview.filter_new()
    self.ps_filter.set_visible_func(self.ps_filter_func)

    self.ps_filter_sorted = Gtk.TreeModelSort(model=self.ps_filter)

    self.votertreeview = Gtk.TreeView.new_with_model(self.ps_filter_sorted)
    for i, coltitle in enumerate(["ID", "WARD", "PS NO", "SR NO", "NAME", "RELATION'S NAME", "SEX", "AGE", "ADDRESS",
                                  "PSTATION", "IDCARD NO"]):
        rendered = Gtk.CellRendererText(foreground = "blue")
        column = Gtk.TreeViewColumn(coltitle, rendered, text=i)
        column.set_sort_column_id(i)
        self.votertreeview.append_column(column)

    def filter_btn_clicked(self, button):
        self.get_typed_filter = self.filter_text.get_text().strip()
        if self.get_typed_filter == "":
            self.current_ps_filter = "None"
        else:
            self.current_ps_filter = self.get_typed_filter
        self.ps_filter.refilter()
        self.text_Filtered_records = "\tFiltered Records : " + str(self.listfortreeview.iter_n_children())
        self.label_Filtered_records.set_text(self.text_Filtered_records)
        self.label_Filtered_records.show()

    def ps_filter_func(self, model, iter, data):

        if self.current_ps_filter is None or self.current_ps_filter == "None":
            return True
        elif self.combo_text == "PS No.":
            return model[iter][2] == self.current_ps_filter

这里 - win.result是一个元组列表

self.filter_text是一个条目,用户在其中输入过滤器应用于第3列的字符串

self.combo_text是一个组合框,用户可以通过该框选择要打开的列(为简单起见,考虑过滤器将始终应用于第3列)

1 个答案:

答案 0 :(得分:2)

您在错误的对象上调用该函数。此外,可以使用内置len函数代替iter_n_children

您只需致电self.listfortreeview.iter_n_children(),而不是致电len(self.ps_filter)listfortreeview未过滤的 ListStore对象。当然,您总是可以从中获得元素的总数。