我正在尝试创建一个列表,每行都有一个checkButton,在此列表的顶部,我希望选择/取消全选选项。问题是我什至无法弄清楚如何在CheckButtons之间迭代以使用类似'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)
答案 0 :(得分:0)
问题:选择列表框内的所有倍数CheckButton的按钮
...我什至无法弄清楚如何在CheckButtons之间进行迭代
基本上可以做到:
for boxrow in listbox2:
定义您自己的ListBoxRow
,它继承自Gtk.ListBoxRow
class ListBoxRow(Gtk.ListBoxRow):
def __init__(self, label, **kwargs):
super().__init__(**kwargs)
定义窗口小部件,并将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)
定义class methode checkbutton(...
以进行设置并获取 active state
CheckButton
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()
让check.connect(...
开心...
def on_check_marked(self, event):
print('on_check_marked({})'.format(event))
用法:
将您的ListBoxRow(...
添加到listbox2
for label in ['Item1','Item2','Item3']:
row = ListBoxRow(label=label)
listbox2.add(row)
根据传递的ListbBox
,迭代CheckButton
以在ListBoxRow
中设置全部 CheckButton active state
。
def mark_all(self, checkbox):
for boxrow in self.listbox2:
boxrow.checkbutton(state=checkbox.get_active())
使用Python测试:3.5-gi .__ version __:3.22.0