[pygtk] Re: Cannot subclass gtk.Widget

Paul Pogonyshev pogonyshev at gmx.net
Fri Mar 9 10:04:08 WST 2007


Jeffrey Barish wrote:
> I still don't understand what to do if my widget is composed of other
> widgets (rather than Cairo drawing and text).

Normally, you just use boxes or something else to align those widgets.
Or subclass e.g. gtk.HBox.  However, if you feel adventurous, you can
play with this incomplete but working example:

import gobject
import gtk

class CustomContainer (gtk.Container):

    def __init__(self, child):
        gtk.Container.__init__(self)

        self.__child = child
        self.__child.set_parent (self)

        self.set_flags (gtk.NO_WINDOW)


    def do_size_request (self, requisition):
        requisition.width, requisition.height = self.__child.size_request ()

    def do_size_allocate (self, allocation):
        self.__child.size_allocate (allocation)


    def do_forall (self, include_internals, callback, user_data):
        callback (self.__child, user_data)


gobject.type_register (CustomContainer)


window = gtk.Window ()
window.add (CustomContainer (gtk.Label ('test')))

window.show_all ()
window.present ()

window.connect ('destroy', lambda window: gtk.main_quit ())

gtk.main ()


More information about the pygtk mailing list