在 ListBox 中制作一个 Select All 按钮的倍数 CheckButton

Make a Select All button of multiples CheckButton inside a ListBox

我正在尝试制作一个列表,其中每一行都有一个 checkButton,在这个列表的顶部我想要一个 Select/Deselect All 选项。问题是我什至无法弄清楚如何在 CheckButton 之间迭代以使用 'Gtk.CheckButton.Set_activate(True)' 之类的东西。我完全迷失了这个问题。

这是我目前的代码

class ListChapters(Gtk.Window):

def __init__(self):
    Gtk.Window.__init__(self, title="List of Itens")
    self.set_border_width(10)

    box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
    self.add(box_outer)

    listbox = Gtk.ListBox()
    listbox.set_selection_mode(Gtk.SelectionMode.NONE)
    box_outer.pack_start(listbox,False, False, 0)

    row = Gtk.ListBoxRow()
    hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
    row.add(hbox)
    label = Gtk.Label('Marcar/Desmarcar tudo.', xalign=0)
    checkall = Gtk.CheckButton()
    hbox.pack_start(label, True, True, 0)
    hbox.pack_end(checkall, False, True, 0)
    listbox.add(row)
    checkall.connect("toggled", self.mark_all)

    listbox2 = Gtk.ListBox()
    listbox2.set_selection_mode(Gtk.SelectionMode.NONE)
    box_outer.pack_start(listbox2, True, True, 0)

    index = ['Item1','Item2','Item3']

    for i in index:
        row = Gtk.ListBoxRow()
        hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
        row.add(hbox)
        cap = Gtk.Label(i, xalign=0)
        check = Gtk.CheckButton()
        hbox.pack_start(cap, True, True, 0)
        hbox.pack_start(check, False, True, 0)
        listbox2.add(row)
        check.connect("toggled", self.on_check_marked)

Question: Select All button of multiples CheckButton inside a ListBox
... i can't even figure it out how to iterate between the CheckButtons

基本上你可以做到:

for boxrow in listbox2:

Gtk 3.0 (3.24.5) 文档:


  1. 定义您拥有 ListBoxRow 继承自 Gtk.ListBoxRow

    class ListBoxRow(Gtk.ListBoxRow):
        def __init__(self, label, **kwargs):
            super().__init__(**kwargs)
    
  2. 定义您的小部件并使 self.check 成为 class attribute

            hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
            self.add(hbox)
    
            cap = Gtk.Label(label, xalign=0)
            hbox.pack_start(cap, True, True, 0)
    
            self.check = check = Gtk.CheckButton()
            hbox.pack_start(check, False, True, 0)
    
            check.connect("toggled", self.on_check_marked)
    
  3. 定义一个class methode checkbutton(...setgetactive stateCheckButton

        def checkbutton(self, state=None):
            print('checkbutton({})'.format(state))
            if state is True:
                self.check.set_active(state)
            elif state is False:
                self.check.set_active(state)
            else:
                return self.check.get_active()
    
  4. check.connect(...开心...

        def on_check_marked(self, event):
            print('on_check_marked({})'.format(event))
    

Usage:

  1. 将您的 ListBoxRow(... 添加到 listbox2

            for label in ['Item1','Item2','Item3']:
                row = ListBoxRow(label=label)
                listbox2.add(row)
    
  2. 迭代ListbBox根据传入的CheckButton active state设置所有ListBoxRowCheckButton

        def mark_all(self, checkbox):
            for boxrow in self.listbox2:
                boxrow.checkbutton(state=checkbox.get_active())
    

测试 Python:3.5 - gi.__version__:3.22.0