Python: How to convert GTK2 DrawingArea "expose_event" action into GTK3 "draw" action?

I have existing Python code which was written for GTK2.

A dynamically created image is kept and modified in a “GdkPixbuf” object. Whenever it changed “queue_draw()” is called.

So far a “expose_event” action handler then did the actual display which no longer works with GTK3.

My first guess is that I have to replace “expose_event” with “draw” to bring my GdkPixbuf to display. So far the pixmap is drawn with:

obj.window.draw_pixbuf(obj.style.fg_gc[Gtk.StateType.NORMAL], self.preview_pixmap, 0, 0, 0, 0)

Where “obj” is the reference to the “DrawingArea” element.

This worked for GTK2 but I completely fail to adapt this to GTK3. If I’m right then the second argument to my “draw” handler will be directly the cairo surface but how do I get my pixbuf onto the cairo surface?

Thank you very much in advance.

Hi,

Don’t call draw() yourself.
Gtk will call it for you (when it needs to show something or after you call queue_draw()), and will provide the cairo context.

For drawing the pixbuf, look at Gdk.cairo_set_source_pixbuf :

def draw(cairo_context):
    Gdk.cairo_set_source_pixbuf(cairo_context, pixbuf, 0, 0)
    cairo_context.paint()
    return True