Skip to content
Advertisement

How do you add an inline image to PyGObject 3?

I want to put inline images into my PyGObject 3 programs so they won’t have to be loaded from disk. YoLinux shows how to edit an XPM file so its text can be inserted into Python code. The free e-book “PyGTK 2.0 Tutorial” by John Finlay gives two code examples. All that’s for PyGTK 2 not gi / PyGObject 3. It doesn’t have to be XPM data, that’s just an easy way to do it in PyGTK 2. Below is an edited version of example pixmap.py

How could this working example be changed to PyGObject 3 or is there another working example of an easier way to add an inline image to PyGObject 3?

import pygtk
pygtk.require('2.0')
import gtk

xpm_data = [
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "
]

class PixmapExample:
    def close_application(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", self.close_application)
        window.set_border_width(10)
        window.show()

        pixmap, mask = gtk.gdk.pixmap_create_from_xpm_d(window.window, 
                                                        None, 
                                                        xpm_data)
        
        image = gtk.Image()
        image.set_from_pixmap(pixmap, mask)
        image.show()

        button = gtk.Button()
        button.add(image)
        window.add(button)
        button.show()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    PixmapExample()
    main()

Advertisement

Answer

This is a straightforward port of your example to PyGObject 3. The solution makes use of the GdkPixbuf.Pixbuf.new_from_xpm_data() function to create the image.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf

xpm_data = [
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "
]

class PixmapExample:
    def close_application(self, widget, event, data=None):
        Gtk.main_quit()
        return False

    def __init__(self):
        window = Gtk.Window()
        window.connect("delete_event", self.close_application)
        window.set_border_width(10)
        window.show()

        pixbuf = GdkPixbuf.Pixbuf.new_from_xpm_data(xpm_data)
        image = Gtk.Image()
        image.set_from_pixbuf(pixbuf)
        image.show()

        button = Gtk.Button()
        button.add(image)
        window.add(button)
        button.show()

def main():
    Gtk.main()
    return 0

if __name__ == "__main__":
    PixmapExample()
    main()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement