Overriding virtual methods in PyGObject

2024/11/16 8:33:32

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?

Answer

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, 100def do_get_preferred_height(self):print("test")return 100, 100win = 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.

https://en.xdnf.cn/q/71691.html

Related Q&A

How to find if two numbers are consecutive numbers in gray code sequence

I am trying to come up with a solution to the problem that given two numbers, find if they are the consecutive numbers in the gray code sequence i.e., if they are gray code neighbors assuming that the …

How do I get data from selected points in an offline plotly python jupyter notebook?

Example code:from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplotimport plotly.graph_objs as goimport numpy as npN = 30 random_x = np.random.randn(N) random_y = np.random.randn…

Set background colour for a custom QWidget

I am attempting to create a custom QWidget (from PyQt5) whose background colour can change. However, all the standard methods of setting the background colour do not seem to work for a custom QWidget c…

Plotly: How to set up a color palette for a figure created with multiple traces?

I using code below to generate chart with multiple traces. However the only way that i know to apply different colours for each trace is using a randon function that ger a numerico RGB for color. But r…

Which implementation of OrderedDict should be used in python2.6?

As some of you may know in python2.7/3.2 well get OrderedDict with PEP372 however one of the reason the PEP existed was because everyone did their own implementation and they were all sightly incompati…

Signal Handling in Windows

In Windows I am trying to create a python process that waits for SIGINT signal.And when it receives SIGINT I want it to just print a message and wait for another occurrence of SIGINT.So I used signal h…

Python tkinter.filedialog askfolder interfering with clr

Im mainly working in Spyder, building scripts that required a pop-up folder or file Browse window.The code below works perfect in spyder. In Pycharm, the askopenfilename working well, while askdirector…

Run a function for each element in two lists in Pandas Dataframe Columns

df: col1 [aa, bb, cc, dd] [this, is, a, list, 2] [this, list, 3]col2 [[ee, ff, gg, hh], [qq, ww, ee, rr]] [[list, a, not, 1], [not, is, this, 2]] [[this, is, list, not], [a, not, list, 2]]What Im tryin…

cannot filter palette images error when doing a ImageEnhance.Sharpness()

I have a GIF image file. I opened it using PIL.Image and did a couple of size transforms on it. Then I tried to use ImageSharpness.Enhance() on it...sharpener = PIL.ImageEnhance.Sharpness(img) sharpene…

Is there a PyPi source download link that always points to the lastest version?

Say my latest version of a package is on PyPi and the source can be downloaded with this url:https://pypi.python.org/packages/source/p/pydy/pydy-0.3.1.tar.gzId really like to have a url that looks like…