I have a small application that uses a DrawingArea to draw a simple map using PyGObject and GTK3.
I load a Pixbuf using
from gi.repository import Gtk, GdkPixbuf
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size("logo.png", 25, 25)
and then try to draw it in the DrawingArea‘s draw event signal
def draw(self, widget, context):
window = widget.get_window()
ctx = window.cairo_create()
ctx.set_source_pixbuf(pixbuf, 0, 0)
but I get the error message
"AttributeError: 'cairo.Context' object has no attribute 'set_source_pixbuf'"
If I’m reading the Gtk2 to Gtk3 migration guide correctly, this should work.
What am I doing wrong?
The new draw signal uses a callback that already passes the cairo context as a parameter, you don’t need to do stuff like
window = widget.get_window()like you did in PyGtk to get the cairo context while attending the expose-event signal. In PYGObject is simpler:That is if you don’t need the PixBuf, but if you need it for something else you have several options:
If you choose the awful second option here is how:
Note that create_for_data() is not yet available for Python3, only for Python2.
Check also my answer on how to use a double buffer in PyGObject if this is what you’re trying to achieve: Drawing in PyGobject (python3)
Kind regards