[cairo] problem refreshing cairo surface

Zoltán Vörös zvoros at gmail.com
Sat Nov 12 13:25:42 PST 2011

Hi All,

I believe, this is a trivial problem, but lacking experience, I can't 
solve it, and I was wondering whether someone could lend a hand here. In 
short, I have a python class that just initialises a drawing area, and I 
would like to use it from somewhere else. It looks like this

class Preview:
     def __init__(self):
         self.da = Gtk.DrawingArea()
         self.da.set_size_request(400, 600)
         self.da.connect('configure-event', self.configure_event)
         self.da.connect('draw', self.on_draw)

     def configure_event(self, da, event):
         allocation = da.get_allocation()
         self.surface = 

         self.cairo_ctx = cairo.Context(self.surface)
         self.cairo_ctx.set_source_rgb(1, 1, 1)
         return True

     def on_draw(self, da, cairo_ctx):
         cairo_ctx.set_source_surface(self.surface, 0, 0)
         return True


Now, in a gui I have a button that calls the following function:

     def on_load(self, widget, data=None):

         self.DA = Preview()
         self.widget('box_preview').pack_start(self.DA.da, True, True, 0)

         self.DA.cairo_ctx.set_source_rgb(1, 0, 0)
         rect = self.DA.da.get_allocation()
         self.DA.da.get_window().invalidate_rect(rect, False)

         print 'here'

The function is executed without errors, and at the end 'here' is 
printed. Yet, the drawing area is not red as I would expect it from the 
line self.DA.cairo_ctx.set_source_rgb(1, 0, 0), but white, as done in 
the initialisation function. Of course, I could pass red as an argument 
to the init function, but that can't be a long-term solution, because I 
actually would like to manipulate the canvas from the main program.
I think I would formulate my question as what should one do in order to 
update the canvas from the main function?
Many thanks for any hints,

More information about the cairo mailing list