7

环境:Python3

图书馆:

from gi.repository import Gtk, Gdk
import cairo

我想从文件创建一个“pixbuf”,但该方法在 Gdk3 中不再存在。

pb = Gdk.pixbuf_new_from_file('sunshine.png')
Gdk.cairo_set_source_pixbuf(cr, pb, 0, 0)

结果:AttributeError:'gi.repository.Gdk'对象没有属性'pixbuf_new_from_file'

有人知道如何用 Gdk3 做到这一点吗?似乎Gdk3只支持这些方法:

gdk_pixbuf_get_from_window
gdk_pixbuf_get_from_surface

更新

自己发现了:

image = Gtk.Image()
image.set_from_file('sunshine.png')
pixbuf = image.get_pixbuf()
Gdk.cairo_set_source_pixbuf(cr, pixbuf, 10, 10)
4

2 回答 2

21

这个问题很老,但也许它可以帮助某人:

from gi.repository import GdkPixbuf
pixbuf = GdkPixbuf.Pixbuf.new_from_file('sunshine.png')

在 GdkPixbuf._version == '2.0' 上测试

于 2013-09-24T23:19:43.133 回答
1

我对python不太了解,但一般来说你不应该直接使用gtk图像来加载文件。由于错误处理,请改用 GdkPixbuf。没有属性(在 python 中)“pixbuf new from file”的原因是因为对于大部分 GdkPixbuf 处理,仍然使用(在 C 中)gdk-pixbuf-2.0。为什么他们没有移植或仍然保留它,我不知道。正如 Schcriher 所指出的,在 python 中,您必须找到“gdk pixbuf”模块而不是使用 GDK3。

于 2014-04-29T10:33:28.603 回答