How to use GObject.GEnum as property type in PyGobject (original) (raw)
February 10, 2025, 6:18am 1
How can I use GObject.GEnum
as a property type in PyGobject. For example Gtk.Box
has a property halign
of type Gtk.Align
enum. Now let’s say I’ve the following:
# custom enum
class example_enum(GObject.GEnum):
PROP0 = 0
PROP1 = 1
# custom subclass
class custom_box(Gtk.Box):
__gproperties__: {
"example_prop": # property_of_type_example_enum
}
# other_stuffs
How can I set example_prop
here?
jamesh (James Henstridge) February 13, 2025, 7:37am 2
It isn’t currently possible to register new enumeration types with current stable releases of PyGObject. Support was merged to main earlier today though:
With those changes, your example_enum
class definition will automatically register a new GType for the enumeration with the two values you’ve defined. You should then be able to use it the same way as any other registered enumeration:
>>> from gi.repository import GObject
>>> class example_enum(GObject.GEnum):
... PROP0 = 0
... PROP1 = 1
...
>>> example_enum.__gtype__
<GType __main__+example_enum (289342256)>
>>> example_enum.PROP0
<example_enum.PROP0: 0>
>>> example_enum.PROP1
<example_enum.PROP1: 1>
Under the covers, GEnum
and GFlags
are now subclasses of the standard library enum.IntEnum
and enum.IntFlag
classes respectively. A custom metaclass is used to hook into the attempt to create a subclass: it notices that example_enum
doesn’t have a GType, so registers a new one based on the declared enumeration values.
raVen (Abir Halder) February 17, 2025, 4:28pm 3
Thanks for the reply. Can you please tell me how can I add a GObect.GEnum
as a property type with required parameters? In my case for example_enum
I’m trying to do it like this:
__gproperties__ = {
"example_prop": (
example_enum.__gtype__,
"example property",
"An example property",
GObject.ParamFlags.READABLE
)
}
And it’s giving an error TypeError: function takes exactly 1 argument (0 given)
system (system) Closed March 19, 2025, 4:28pm 4
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.