[cairo] Keeping previously drawn shapes after 'draw' event?

desktopdave desktopdave at gmx.com
Fri Apr 3 16:54:17 UTC 2020


I am trying to port a tkinter paint app(ms paint clone) to
pygobject/pygtk. Tkinter uses a sophisticated canvas widget, I am trying
to figure out how to emulate it using Gtk.DrawingArea.

I have worked out how to get a solid line(rectangle, oval etc) that
shrinks/grows as I move the mouse - before button1 release, here's the
basic code.

def draw_brush(self, da, event):
         """Draw something"""
         ctx = cairo.Context(self.surface)

         # Set white background
         ctx.set_source_rgb(1, 1, 1)

         # Draw selected shape chosen by user


My problem is that by setting the background white on each draw, I get
the solid growing/shrinking line(rectangle etc) effect needed, but it
also means that any previously drawn shape is erased at next draw event.
I need to somehow store the previous draws to the surface and reinstate
them on each draw event, so the user can paint a picture.

The method 'execute_selected_method(ctx)' simply selects the method for
the button that the user clicks on in a toolbox eg if the user selects
'line' button then selected method will be:

def draw_line(self, ctx):
         ctx.move_to(self.start_x, self.start_y)
         ctx.line_to(self.end_x, self.end_y)

I must admit that my understanding of cairo graphics is limited but I
get the basic principles.

Thanks for any help. If you need me to explain anything just reply and I
will - I can email my current pygobject(pygtk) source code if that helps.

Cheers all.

More information about the cairo mailing list