我已经创建了一个GtkFlowBox,我已经在其中添加了一大堆按钮,我试图通过一个功能来捕捉这些按钮的点击,目前只需输出所选按钮。但是,每个按钮的实际可点击区域是微小的 - 您需要在按钮的角落单击以进行注册。
我似乎找不到任何选项来扩展GtkFlowBox类中的子类的可点击区域,也不会扩展到GtkButton类中。以下是演示的最低工作示例。
class NotebookWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="FlowBox Test")
#Create Application Window
self.set_border_width(10)
self.set_default_size(800, 600)
self.set_position(Gtk.WindowPosition.CENTER)
#Add external container (box)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
#Add tabbed window
self.nbook = Gtk.Notebook()
vbox.pack_start(self.nbook, True, True, 0)
self.nbook.show()
#Add Clubs tab
frame = Gtk.Frame()
frame.show()
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
club_box = Gtk.FlowBox()
club_box.set_valign(Gtk.Align.START)
club_box.set_max_children_per_line(30)
club_box.set_selection_mode(Gtk.SelectionMode.SINGLE)
club_box.set_activate_on_single_click(True)
for club in range(0, 55):
button = Gtk.Button(club)
club_box.add(button)
vbox.pack_start(club_box, True, True, 0)
self.out_message = Gtk.Label("\n")
vbox.add(self.out_message)
label = Gtk.Label()
label.set_markup("<b><big>Clubs</big></b>")
frame.add(vbox)
club_box.connect("child-activated", self.on_club_details)
self.nbook.append_page(frame, label)
def on_club_details(self, club_box, child):
print "clicked club box %d" % child.get_index()
win = NotebookWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
答案 0 :(得分:0)
我认为你真正想要的是在按钮上注册点击,而不是在激活按钮时注册。单击按钮后,不一定会激活该按钮。
我简化了你的代码,但这是我认为你想要实现的目标:
class NotebookWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="FlowBox Test")
#Create Application Window
self.set_border_width(10)
self.set_default_size(400, 400)
self.set_position(Gtk.WindowPosition.CENTER)
#Add tabbed window
self.nbook = Gtk.Notebook()
self.add(self.nbook)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
club_box = Gtk.FlowBox()
club_box.set_valign(Gtk.Align.START)
club_box.set_max_children_per_line(30)
club_box.set_selection_mode(Gtk.SelectionMode.SINGLE)
club_box.set_activate_on_single_click(True)
self.buttons = []
for club in range(0, 55):
button = Gtk.Button(label=club)
club_box.add(button)
button.connect('clicked', self.on_button_clicked) # new callback
self.buttons.append(button)
vbox.pack_start(club_box, True, True, 0)
self.out_message = Gtk.Label(label="\n")
vbox.pack_start(self.out_message, True, True, 0)
label = Gtk.Label()
label.set_markup("<b><big>Clubs</big></b>")
self.nbook.append_page(vbox, label)
def on_button_clicked(self, button):
for i in range(len(self.buttons)):
if self.buttons[i] == button:
print('button at index {}'.format(i))
break
win = NotebookWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()