[pygtk] Subclassing GObject and virtual functions

Johan Dahlin jdahlin at async.com.br
Fri Aug 11 19:39:27 WST 2006


Samuel Cormier-Iijima skrev:
> Hi John,
>
> Thanks for the reply :-) I've gotten it to work with these virtual
> functions, however the python code is ugly with the required call to
> gobject.type_register and using signals for the vfuncs (you have to
> subclass and implement the method do_on_connected and define
> __gsignals__ instead of just implementing on_connected). I'm not sure
> how hard this would be to implement, are there plans under way to
> allow for things like this?
Signal overrides are different from virtual methods.
The support for overriding virtual methods is relatively new in PyGTK, 
It was introduced
in 2.6 unless I'm mistaken.

You'll only need to define __gsignals__ if you want to override a 
signal, it is not necessary to implement a virtual method. However, you 
should always call gobject.type_register for the subclass even though 
it's not always necessary, it's of good practice.

In other words, to override say expose event, do this:

class MyWidget(gtk.Widget):
   def do_expose_event(self, event):
      pass
gobject.type_register(MyWidget)

For a complete example of a custom widget check out the 
examples/gtk/widget.py file in pygtk cvs/tarball.

Johan




More information about the pygtk mailing list