How to set eyed3.tag.image_data as Gtk.Picture picture

Is there any way to set Gtk.Picture content, neither to the file nor to resource. I have eyed3 parsed audio cover, but I don’t know any way to set it as image in Gtk.Picture. I tried GdkPixbuf, but it does not work.

If image data is in bytes and in a format supported by GTK, then you can can convert them to GLib.Bytes and create a new Gdk.Texture with them (see Gdk.Texture.new_from_bytes ) then you can use that texture as the paintable of the Gtk.Picture.

1 Like

I can’t create a variable of type Gdk.Texture. I am getting this error TypeError: cannot create instance of abstract (non-instantiable) type GdkTexture’`

This is my code:

from gi.repository import Adw
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gdk


@Gtk.Template(resource_path="/com/github/dzheremi/lrcmake/gtk/components/songCard.ui")
class songCard(Gtk.Box):
    __gtype_name__ = 'songCard'

    cover = Gtk.Template.Child()
    song_title = Gtk.Template.Child()
    song_artist = Gtk.Template.Child()

    def __init__(self, track_title, track_artist, track_cover = None):
        super().__init__()
        self.cover.set_filename('/home/dzheremi/Pictures/note.png')
        self.song_title.set_property('label', track_title)
        self.song_artist.set_property('label', track_artist)
        print(track_title, track_artist)
        if track_cover != None:
            image_bytes = GLib.Bytes(track_cover)
            image_texture = Gdk.Texture()
            image_texture.new_from_bytes(image_bytes)
            self.cover.set_property('paintable', image_texture)

You cannot create a Gdk.Texture instance with Python’s normal form constructor; additionally, Gdk.Texture instances are immutable post construction, so you can’t call constructor or setter methods on them:

So how should I then set paintable of cover to Texture if I can’t create it?

I edited your code to show you how, but just in case it wasn’t clear:

if track_cover != None:
    image_bytes = GLib.Bytes(track_cover)
    image_texture = Gdk.Texture.new_from_bytes(image_bytes)
    self.cover.props.paintable = image_texture
2 Likes

Thank you very much! You have kept me interested in creating GTK applications.

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