Python:如何使用Gtk.ListBox.selected_foreach()?

Python: How to use Gtk.ListBox.selected_foreach ()?

我想删除 Gtk.ListBox 中的特定行。

我正在使用 Python 和 Gtk。我有一个 Gtk.ListBox 包含 Gtk.ListBoxRow children。每行都有一个标签。例如,我想删除带有 "test" 标签的行。为此,我想浏览 Gtk.ListBox 并为每一行读取标签并确定它是否等于 "test"。如果是,则应删除该行。为此,我考虑过选择框的所有行,然后使用 selected_foreach 函数获取行的标签,但我对如何使用此函数感到有点困惑。

class List(Gtk.ListBox):
     def __init__(self,builder,object):
          self.size = 0
          self.users_list_box = self.builder.get_object(object)

     def test(self,chemin,iter,donnees)
          print("This is a test")

class ABox(Gtk.VBox):
     def __init__(self,builder):
          self.list = List(self.builder,"list")

     def myfunction():

          self.list.users_list_box.select_all()
          self.list.users_list_box.selected_foreach(self.list.test)

我希望消息 "This is a test" 的打印次数与列表框中的行一样多,但没有显示任何内容。

GtkListBox 是 GtkContainer 的子类,这就是为什么您可以使用 Gtk.Container.foreach. GtkListBox consists 遍历所有行的原因 GtkListBoxRows,它们是 GtkBin 的子类。您可以使用 Gtk.Bin.get_child to get access to the widget inside GtkListBoxRow. After that you can Gtk.Container.remove 不需要的行。