Get Enum value with PyGObject

I’m trying to replicate https://gitlab.gnome.org/GNOME/libadwaita/-/blob/main/demo/pages/flap in Python with PyGObject.

I’ve managed to create the all the UI parts but have some problem with the logic how to populate some of the drops down. One example would be:

                            <child>
                              <object class="AdwComboRow">
                                <property name="title" translatable="yes">Fold Policy</property>
                                <property name="selected" bind-source="flap" bind-property="fold-policy" bind-flags="sync-create|bidirectional"/>
                                <property name="model">
                                  <object class="AdwEnumListModel">
                                    <property name="enum-type">AdwFlapFoldPolicy</property>
                                  </object>
                                </property>
                                <property name="expression">
                                  <closure type="gchararray" function="fold_policy_name"/>
                                </property>
                              </object>
                            </child>

Where the C code looks like

static char *
fold_policy_name (AdwEnumListItem *item,
                  gpointer         user_data)
{
  switch (adw_enum_list_item_get_value (item)) {
  case ADW_FLAP_FOLD_POLICY_NEVER:
    return g_strdup (_("Never"));
  case ADW_FLAP_FOLD_POLICY_ALWAYS:
    return g_strdup (_("Always"));
  case ADW_FLAP_FOLD_POLICY_AUTO:
    return g_strdup (_("Auto"));
  default:
    return NULL;
  }
}

I’m trying something like this in Python

    @Gtk.Template.Callback()
    def fold_policy_name(self, item):
        if item == 'ADW_FLAP_FOLD_POLICY_NEVER':
            return 'Never'
        elif item == 'ADW_FLAP_FOLD_POLICY_ALWAYS':
            return 'Always'
        elif item == 'ADW_FLAP_FOLD_POLICY_AUTO':
            return 'Auto'
        else:
             return NULL

Which doesn’t work since item is not a string <Adw.EnumListItem object at 0x7f1cf3ba1900 (AdwEnumListItem at 0x5585e4660750)>

How can I do adw_enum_list_item_get_value (item) in Python to be able to do the compare?

Ones assumes you want to compare to the enum, not a string:

    @Gtk.Template.Callback()
    def fold_policy_name(self, item):
        if item.value == Adw.FlapFoldPolicy.NEVER:
            return 'Never'
        elif item.value == Adw.FlapFoldPolicy.ALWAYS:
            return 'Always'
        elif item.value == Adw.FlapFoldPolicy.AUTO:
            return 'Auto'
        else:
             return None

I’ve tried with ìtem.value` but it returns.

    if item.value == Adw.FlapFoldPolicy.NEVER:
AttributeError: 'EnumListItem' object has no attribute 'value'

The value GObject property of an item in an Adw.EnumListModel can be accessed using the props meta-object, i.e.

if item.props.value == Adw.FlapFoldPolicy.NEVER:
    return 'Never'

or using the get_value() method:

if item.get_value() == Adw.FlapFoldPolicy.NEVER:
    return 'Never'
1 Like

Thanks! That worked. I was looking for the solution in https://pygobject.readthedocs.io/ but couldn’t find it. Am I looking in the wrong place or am I looking for the wrong thing?

The props metaobject is documented on pygobject’s website.

For the API references, you can check them on the PyGI website, but sadly it does not include the latest GTK 4.0 and libadwaita references.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.