如何使用GTK3和PyGObject绘制GdkPixbuf

gni*_*irx 9 python pygobject gtk3

我有一个小应用程序,使用a DrawingArea来绘制一个简单的地图使用PyGObjectGTK3.

我加载一个Pixbuf使用

from gi.repository import Gtk, GdkPixbuf
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size("logo.png", 25, 25)
Run Code Online (Sandbox Code Playgroud)

然后尝试在DrawingArea绘制事件信号中绘制它

def draw(self, widget, context):
    window = widget.get_window()
    ctx = window.cairo_create()
    ctx.set_source_pixbuf(pixbuf, 0, 0)
Run Code Online (Sandbox Code Playgroud)

但我收到错误信息

"AttributeError: 'cairo.Context' object has no attribute 'set_source_pixbuf'"
Run Code Online (Sandbox Code Playgroud)

如果我正确阅读Gtk2到Gtk3迁移指南,这应该可行.我究竟做错了什么?

Hav*_*vok 10

新的绘制信号使用一个已经将cairo上下文作为参数传递的回调,你不需要window = widget.get_window()像在PyGtk中那样做一些东西,以便在参与expos -event信号时获得cairo上下文.在PYGObject中更简单:

import cairo

class Foo(object):
    def __init__(self):

       (...)
        self.image = cairo.ImageSurface.create_from_png('logo.png')
       (...)

    def draw(self, widget, context):
        if self.image is not None:
            context.set_source_surface(self.image, 0.0, 0.0)
            context.paint()
        else:
            print('Invalid image')
        return False
Run Code Online (Sandbox Code Playgroud)

也就是说,如果您不需要PixBuf,但如果您需要其他东西,您可以选择以下几种:

  1. 将两个对象都放在内存中.如果两者都是从PNG加载的,那么除了浪费内存之外,应该没有太多问题.
  2. 将GdkPixbuf转换为PIL Image,然后将PIL Image转换为数据数组,然后使用create_for_data()从该数据数组创建Cairo ImageSurface.牦牛:SI不知道更好,对不起:S
  3. 使用hock提出的Gdk.cairo_set_source_pixbuf().这似乎是在ImageSurface中绘制Pixbuf的正确方法,但它完全是unpythonic(这就是为什么我讨厌这个Introspection的东西,所有看起来像C,就像一个坏的C端口).

如果你选择了糟糕的第二个选项,那么如何:

import Image
import array
from gi.repository import Gtk, GdkPixbuf

width = 25
height = 25
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size('logo.png', width, height)
pil_image = Image.fromstring('RGBA', (width, height), pixbuf.get_pixels())
byte_array = array.array('B', pil_image.tostring())
cairo_surface = cairo.ImageSurface.create_for_data(byte_array, cairo.FORMAT_ARGB32, width, height, width * 4)
Run Code Online (Sandbox Code Playgroud)

需要注意的是create_for_data()尚未提供Python3,只为Python2.

如果这是你想要实现的,请查看我在PyGObject中如何使用双缓冲区的答案:在PyGobject中绘图(python3)

亲切的问候

  • +1"但它完全是unpythonic(这就是为什么我讨厌这种内省的东西,所有看起来像C,就像一个糟糕的C端口)." (2认同)

gni*_*irx 7

以下似乎做了这个工作:

def draw(self, widget, context):
    Gdk.cairo_set_source_pixbuf(context, self.pixbuf, 0, 0)
    context.paint()
Run Code Online (Sandbox Code Playgroud)

一个问题仍然存在:这是首选的做事方式吗?