[cairo] Can't draw more

Kalle Vahlman kalle.vahlman at gmail.com
Sat Nov 24 03:24:39 PST 2007

2007/11/24, Gary Jaffe <gfj555 at gmail.com>:
> I'm new to cairo, and I wrote a small pygtk application that uses cairo
> to draw a background on a gtk.DrawingArea.  Drawing the background works
> fine, but I also have a button that when clicked does some more drawing
> on the background.  When I execute the context.stroke() statement
> following the button click, I get the following error.
> cairo.Error: the target surface has been finished
> Obviously, I'm missing something here.

Judging by the error, you are hanging on to the cairo context created
in the expose handler and using it outside the handler. The way GTK+
does the expose functions is that it creates a temporary drawable to
take in the drawing and then updates it all in one go to the real
drawable. Thus you created a cairo surface for the temporary drawable
and it already went away.

Drawing directly outside the expose handler is not a good idea with
GTK+ anyway, as it brings problems with preserving the contents (GTK+
likely will quickly overwrite anything you draw outside the handler).

>  Can someone please clue me in on
> how to draw more on the background after the background is already drawn
> or point me to some documentation that will tell me?

The "correct" way to draw something in GTK+ is to invalidate the area
you want to draw to and then handle that in the eventually incoming
expose event. It *is* a bit clunky and makes the expose handler more
complex, but at least you get automatical double-buffering with the


Oh, and just in case you were wondering, this doesn't mean you need to
redraw the whole widget every time. You should always check the
exposed area from the event structure you get in the expose handler to
see what part of the widget needs redrawing and at minimum set the
cairo clip area to that to minimize the work done.

Kalle Vahlman, zuh at iki.fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi

More information about the cairo mailing list