在PyGObject中覆盖虚拟方法 [英] Overriding virtual methods in PyGObject
问题描述
我试图执行高宽几何管理在GTK中为我的自定义Widget使用Python。我的小部件是来自 Gtk.DrawingArea
的子类,并绘制了图像的某些部分。
据我了解GTK文档(上面的链接)我必须实现以下4种方法:
$ b $ ul
>
现在想知道在Python中实现这一点。
我试过这个:
from gi.repository import Gtk
class Patch(Gtk.DrawingArea):
def __init __(self,model,image,position):
super(Patch,self).__ init __()
#...
def get_preferred_width(self,* args,** kargs):
print(test)
def get_preferred_height(self,* args,** kargs):
print(test)
def get_preferred_width_for_height(self,* args,** kargs) :
print(t est)
def get_preferred_height_for_width(self,* args,** kargs):
print(test)
但是这些方法不会被调用。在C中,你可以定义函数并将其设置为如下所示的小部件:
static void
my_widget_get_preferred_height(GtkWidget * widget ,gint * minimal_height,
gint * natural_height)
{
/ * ... * /
}
/ * ... * /
static void
my_widget_class_init(MyWidgetClass * class)
{
/ * ... * /
GtkWidgetClass * widget_class = GTK_WIDGET_CLASS(class);
widget_class-> get_preferred_height = my_widget_get_preferred_height;
/ * ... * /
}
Python?
您必须命名像 do_virtual_method
:
from gi.repository import Gtk
class Patch(Gtk.DrawingArea):
def __init __(self) :
super(Patch,self).__ init __()
def do_get_preferred_width(self):
print(test)
return 100,100
def do_get_preferred_height(self):
print(test)
return 100,100
win = Gtk.Window()
win。添加(Patch())
win.connect('destroy',Gtk.main_quit)
win.show_all()
Gtk.main()
请注意,您还必须返回虚拟方法要求您返回的值,否则您将得到一个模糊的错误。
I'm trying to implement the Heigh-for-width Geometry Management in GTK with Python for my custom Widget. My widget is a subclass from
Gtk.DrawingArea
and draws some parts of an Image.As I understood the GTK Docs (link above) I have to implement the following 4 methods:
- GtkWidgetClass.get_preferred_width()
- GtkWidgetClass.get_preferred_height()
- GtkWidgetClass.get_preferred_height_for_width()
- GtkWidgetClass.get_preferred_width_for_height()
Now wondering where to implement this in Python.
I tried this:
from gi.repository import Gtk
class Patch(Gtk.DrawingArea):
def __init__(self, model, image, position):
super(Patch,self).__init__()
#…
def get_preferred_width(self, *args, **kargs):
print("test")
def get_preferred_height(self, *args, **kargs):
print("test")
def get_preferred_width_for_height(self, *args, **kargs):
print("test")
def get_preferred_height_for_width(self, *args, **kargs):
print("test")
But the methods don't get called. In C you define the functions and set it to the widget like this:
static void
my_widget_get_preferred_height (GtkWidget *widget, gint *minimal_height,
gint *natural_height)
{
/* ... */
}
/* ... */
static void
my_widget_class_init (MyWidgetClass *class)
{
/* ... */
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
widget_class->get_preferred_height = my_widget_get_preferred_height;
/* ... */
}
How is this done in Python?
You have to name the methods like do_virtual_method
:
from gi.repository import Gtk
class Patch(Gtk.DrawingArea):
def __init__(self):
super(Patch,self).__init__()
def do_get_preferred_width(self):
print("test")
return 100, 100
def do_get_preferred_height(self):
print("test")
return 100, 100
win = Gtk.Window()
win.add(Patch())
win.connect('destroy', Gtk.main_quit)
win.show_all()
Gtk.main()
Note that you also have to return the values that the virtual method requires you to return, otherwise you'll get a cryptic error.
这篇关于在PyGObject中覆盖虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!