2

I am trying to write a GTK widget in Python that is a subclass of gtk.Bin and am not sure how to go about instantiating it. The first few lines of my class look like:

class Completer(gtk.Bin):

    def __init__(self, exts)):

        gtk.Container.__init__(self)
        child = gtk.VBox(spacing=15)
        self.add(child)

I'm not sure how to set the child attribute, hence the code for that. But it hangs up on the line gtk.Container.__init__(self) with the message:

  File "C:\Users\462974\Documents\Local Sandbox\tools\python\packages\GUI\tools\SNCompleter.py", line 133, in __init__
    gtk.Container.__init__(self)
TypeError: cannot create instance of abstract (non-instantiable) type `GtkBin'

It also happens if I call gtk.Bin.__init__. I'm not sure how to initialize this subclass, but there is presumably a way since GTK does have usable subclasses of gtk.Bin.

dpitch40
  • 2,621
  • 7
  • 31
  • 44

1 Answers1

1

You need to register a new gtype for your widget, otherwise it will use the same as the super class, and since it's an abstract class, you won't be able to instantiate it (as the exception indicates).

There're two ways of registering a new gtype:

  1. Using gobject.type_register.
  2. Setting the __gtype_name__ class variable in your class.

Here's an example using the second choice (since I believe is more straigth forward):

class Completer(gtk.Bin):
    __gtype_name__= "Completer"

    def __init__(self, exts, *args, **kwargs):
        super(Completer, self).__init__(*args, **kwargs)
        child = gtk.VBox(spacing=15)
        self.add(child)
asermax
  • 3,053
  • 2
  • 23
  • 28
  • I did that and it now runs successfully, but the widget isn't showing up, even though I pack everything in self.child and call show_all on the top-level application. – dpitch40 Feb 26 '13 at 15:47
  • I had a similar probrem trying to inherit from `GtkBin`, but using `pygobject` instead of `pypgtk`. [Here](http://stackoverflow.com/questions/14253553/extending-from-gtkbin) is the question I made back then, aparently there's some problem in the bindings for `GtkBin`, probably it's the same thing on `pygtk` – asermax Feb 26 '13 at 17:48
  • Sub-classing `gtk.Frame` or `gtk.Viewport` instead of `gtk.Bin` also fixes the issue of the widget not displaying. – Uyghur Lives Matter Apr 26 '13 at 03:22