I think PyGObject’s GObject.Object.bind_property_full is a good fit for my use case: binding a widget to a model object with a transformation involved. However, when trying to use this method, I get the error that my version of PyGObject does not support it. These docs however state that it’s supported since version 2.26. I use it as follows:
import gi
gi.require_version('GObject', '2.0')
from gi.repository import GObject
I’m on Ubuntu 24.04 and my PyGObject version is 3.54.3.
First of all, you should be using the updated API docs. As you can see there, bind_property_full() was subsumed into bind_property(), since Python allows default arguments—and even at the C level, bind_property() is implemented on top of bind_property_full().
The bind_property() method allows you to pass transformation functions to convert a value from the source to the target (transform_to) and vice versa (transform_from); both transformation functions take the GObject.Binding instance and the value to transform, and return the transformed value. For instance:
def _celsius_to_fahrenheit(binding: GObject.Binding, value: float) -> float:
return (value * 1.8) + 32
def _fahrenheit_to_celsius(binding: GObject.Binding, value: float) -> float:
return (value - 32) * 0.56
# Bind source.temp_f to target.temp_c with a conversion
# from source to target and vice versa
source.bind_property("temp-f", target, "temp-c",
GObject.BindingFlags.BIDIRECTIONAL,
_fahrenheit_to_celsius,
_celsius_to_fahrenheit)
Thank you very much. This is exactly what I was looking for.
I indeed found the site of the updated API docs, I landed on a sub-site of it. However, I haven’t found the link to the API reference neither in the header, nor the side bar nor the footer, so I assumed this is only a guide site. I just now checked the home page and found the link in the text there. Maybe it would be useful to put the link to the API reference at some more prominent place?