如何将内联图像添加到 PyGObject 3?

How do you add an inline image to PyGObject 3?

我想将内联图像放入我的 PyGObject 3 程序中,这样它们就不必从磁盘加载。 YoLinux shows how to edit an XPM file so its text can be inserted into Python code. John Finlay 的免费电子书“PyGTK 2.0 教程”提供了两个代码示例。所有这些都是针对 PyGTK 2 而不是 gi / PyGObject 3 的。它不一定是 XPM 数据,这只是在 PyGTK 2 中执行此操作的一种简单方法。下面是示例 pixmap.py

的编辑版本

如何将此工作示例更改为 PyGObject 3,或者是否有另一个工作示例可以更轻松地将内联图像添加到 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()

这是将您的示例直接移植到 PyGObject 3. The solution makes use of the GdkPixbuf.Pixbuf.new_from_xpm_data() 函数来创建图像。

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 的回答后,我整理了我能做的最简单的版本。

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()