简体   繁体   中英

Draw a shape on top of an image using Gtk and pycairo

I have recently started using glade, python gtk+ and cairo to build GUI apps. I have constructed a DrawingArea widget in which I have used pixbuf to paint an image. However when i try to draw shapes on the image, the shapes get drawn behind the image.

def on_draw(self, wid, cr):
    #cr.rectangle(1500, 300, 400, 800)
    #cr.stroke()
    self.pb = GdkPixbuf.Pixbuf.new_from_file_at_scale("image2.jpg", 1920, 1080, True)
    Gdk.cairo_set_source_pixbuf(cr, self.pb, 0, 0)
    cr.paint()

Is there any way possible to get the shapes on top of the image?

Sorry for such a silly question. I am just new to this.

Why don't you just draw the shape ontop of the image instead of behind it?

So, instead of, well, drawing the shape first and then drawing the image, do it the other way around. First draw the image and then the shape.

def on_draw(self, wid, cr):
    self.pb = GdkPixbuf.Pixbuf.new_from_file_at_scale("image2.jpg", 1920, 1080, True)
    Gdk.cairo_set_source_pixbuf(cr, self.pb, 0, 0)
    cr.paint()
    cr.set_source_rgb(0, 0, 0)
    cr.rectangle(1500, 300, 400, 800)
    cr.stroke()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM