简体   繁体   English

Python GTK3继承

[英]Python GTK3 inheritance

How can I inherit a GTK+3 class in python ? 如何在python中继承GTK + 3类? I'm trying to create a inherited class of Gtk.Application and what I got is a segfault. 我正在尝试创建Gtk.Application的继承类,而我得到的是段错误。

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)

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() . 这是预期的,因为您正尝试使用Program.new()gtk_application_new()调用Python包装器。

you should use the Python constructor form: 您应该使用Python构造函数形式:

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: 这实际上会警告您尚未实现GApplication::activate虚拟函数,这可以通过在Program类中重写do_activate虚拟方法来实现:

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! 这将打印Activated! on the console, before quitting. 在控制台上,然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM