pythonpython-3.xgtk4

Having trouble displaying an image in an Adw.AboutDialog in python


I have the following file structure:

.
├── resources.gresource
├── resources.xml
├── texty.py
└── texty.svg

The resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/texty">
    <file>texty.svg</file>
  </gresource>
</gresources>

Compiled using:

glib-compile-resources resources.xml --target=resources.gresource

...and registered in the texty.py init function:

resources = Gio.Resource.load('./resources.gresource')
Gio.resources_register(resources)

Retrieved like this it works:

image = Gtk.Image.new_from_resource('/texty/texty.svg')
box.append(image)

...but not in the AboutDialog code:

about_dialog = Adw.AboutDialog.new()
about_dialog.set_application_name("texty")
about_dialog.set_application_icon("/texty/texty.svg")
about_dialog.present()

I get a 'broken image' displayed. No errors. This works in C-code. Is there something different I need do in python? I tried using a png with the same result.


Solution

  • I got it working after much trial and error. I moved the icon to data/icons/hicolor/scalable/apps, not sure if this was necessary but it seemed more correct:

    .
    ├── data
    │   └── icons
    │       └── hicolor
    │           └── scalable
    │               └── apps
    │                   └── texty.svg
    └── texty.py
    

    I removed the resources.xml file and the compiled resources.gresource. I couldn't get it to work.

    Then I modified the window's init function, removing the resources registration and adding the full icon path to the default icon theme:

    # add path to icons to default icon theme
    icon_dir = os.path.join(os.path.dirname(__file__), 'data', 'icons', 'hicolor', 'scalable', 'apps')
    icon_theme = Gtk.IconTheme.get_for_display(Gdk.Display.get_default())
    icon_theme.add_search_path(icon_dir)
    

    And in the About action's handler, I referenced the icon by name, without the .svg extension, rather than its path:

    def on_about_action_activated(self, action, param=None):
        about_dialog = Adw.AboutDialog.new()
        about_dialog.set_application_name("texty")
        about_dialog.set_application_icon("texty")
        about_dialog.present()