I am trying to create a custom widget with PyGObject. For example I want to create this CustomButton
widget which adds an image and a label in a button (it's just for the example) :
#!/usr/bin/python
#-*- coding: utf-8 -*
from gi.repository import Gtk
class ImageButton(Gtk.Widget):
def __init__(self, label, image):
Gtk.Widget.__init__(self)
self.hbox = Gtk.HBox()
self.label = Gtk.Label(label)
self.image = Gtk.Image.new_from_stock(image, Gtk.IconSize.MENU)
self.hbox.pack_start(self.image, False, False, 3)
self.hbox.pack_start(self.label, False, False, 3)
self.button = Gtk.Button()
self.button.add(self.hbox)
In another file or class, I can use it like that :
button = ImageButton("My label", Gtk.STOCK_HOME)
But when I want to use it, I am obliged to call the button
attribute, like this :
# Connect the "clicked" event
button.button.connect("clicked", on_clicked_button)
# Add the button in a container
window.add(button.button)
It works but it is not practical. How to create a custom widget working like any other widget please :
button = ImageButton("My label", Gtk.STOCK_HOME)
button.connect("clicked", on_clicked_button)
window.add(button)