2

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()
Dave Brunker
  • 1,559
  • 5
  • 15
  • 23

2 Answers2

2

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()
bohrax
  • 1,051
  • 8
  • 20
  • Thanks, bohrax, you're amazing!! You've not only helped me but every beginner who wants to add inline images to their programs! – Dave Brunker Jan 19 '21 at 16:03
  • This gives me that error though: "Inline XPM data is broken: Dimensions do not match data" Edit: Nope, nevermind, was my mistake; I batch-replaced something and that also mangled the XPM. Using the original variant as shown above does work, I can confirm this as-is. – shevy Feb 23 '21 at 21:06
0

After studying bohrax's answer I put together the simplest version I could.

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.    ",
"   .........    ",
"                ",
"                "
]

window = Gtk.Window()

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

window.add(image)

window.connect("destroy", Gtk.main_quit)

window.show_all()
Gtk.main()
Dave Brunker
  • 1,559
  • 5
  • 15
  • 23