如何在python中继承GTK+3类?我正在尝试创建一个继承的Gtk.Application类,我得到的是一个分段错误。
我试过很多次了,但是这样我就错了:
class Program(Gtk.Application):
def __init__(self):
super().__init__(self)
...
prg = Program.new("app_id", flags)发布于 2014-04-22 15:28:39
如果我尝试一下您的代码片段,我实际上得到了:
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这是预期的,因为您试图通过使用gtk_application_new()调用Program.new()的Python包装。
您应该使用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));这实际上会警告您尚未实现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!")在退出之前,这将在控制台上打印Activated!。
https://stackoverflow.com/questions/23207870
复制相似问题