Search code examples
gtk3pygobject

Python Gtk3 creating simple container object with Gtk.Container


I tried to create a basic GTK Container widget with the following code:

from Gtk3Modules import *
from gi.repository.GObject import GObject

class Ex(Gtk.Container):
    pass


btn = Gtk.Button("nss")

ab = Ex()
ab.add(btn)


w = Gtk.Window()
w.add(ab)
w.show_all()

when I launch this script, I get the following fatal error:

(example.py:2642): Gtk-WARNING **: GtkContainerClass::add not implemented for '__main__+Ex'
**
Gtk:ERROR:gtkwidget.c:12365:gtk_widget_real_realize: assertion failed: (!_gtk_widget_get_has_window (widget))
rlwrap: warning: python3 crashed, killed by SIGABRT (core dumped).
rlwrap itself has not crashed, but for transparency,
it will now kill itself with the same signal


warnings can be silenced by the --no-warnings (-n) option
Aborted (core dumped)

Solution

  • Gtk.Container is not a Widget it is an Interface that you have to implement. It is unlikely that is what you want to actually do as implementing a new container is not trivial.

    What you want to use is probably Gtk.Box if you want it to contain multiple children or Gtk.Bin if you only want a single child.