gni*_*irx 9 python pygobject gtk3
我有一个小应用程序,使用a DrawingArea来绘制一个简单的地图使用PyGObject和GTK3.
我加载一个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,但如果您需要其他东西,您可以选择以下几种:
如果你选择了糟糕的第二个选项,那么如何:
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)
亲切的问候
以下似乎做了这个工作:
def draw(self, widget, context):
Gdk.cairo_set_source_pixbuf(context, self.pixbuf, 0, 0)
context.paint()
Run Code Online (Sandbox Code Playgroud)
一个问题仍然存在:这是首选的做事方式吗?