Frage

How can I inherit a GTK+3 class in python ? I'm trying to create a inherited class of Gtk.Application and what I got is a segfault.

I've tried a lot of things, but with this I got a segfault:

class Program(Gtk.Application):
    def __init__(self):
        super().__init__(self)

...
prg = Program.new("app_id", flags)
War es hilfreich?

Lösung

if I try your code snippet I actually get:

Traceback (most recent call last):
  File "pyclass.py", line 12, in <module>
    prg = Program.new("app_id", 0)
TypeError: Application constructor cannot be used to create instances of a subclass Program

which is expected, since you're trying to call the Python wrapper for gtk_application_new() by using Program.new().

you should use the Python constructor form:

class Program(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self,
                                 application_id="org.example.Foo", 
                                 flags=Gio.ApplicationFlags.FLAGS_NONE)

prg = Program()
sys.exit(prg.run(sys.argv));

this will actually warn you that you haven't implemented the GApplication::activate virtual function, which can be achieved by overriding the do_activate virtual method in your Program class:

class Program(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self,
                                 application_id="org.example.Foo",
                                 flags=Gio.ApplicationFlags.FLAGS_NONE)
    def do_activate(self):
        print("Activated!")

this will print Activated! on the console, before quitting.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top