Drawing pixbuf on a drawing area using pygtk and glade

I am trying to make a GTK application in python where I can just draw a loaded image on the screen where I click on it. The way I'm trying to do this is to load the image into a pixbuf file, and then draw that pixbuf on the drawing area.

The main line of code is here:

def drawing_refresh(self, widget, event): #clear the screen widget.window.draw_rectangle(widget.get_style().white_gc, True, 0, 0, 400, 400) for n in self.nodes: widget.window.draw_pixbuf(widget.get_style().fg_gc[gtk.STATE_NORMAL], self.node_image, 0, 0, 0, 0) 

This should just draw pixbuf on the image in the upper left corner, but nothing is displayed except for the white image. I tested that pixbuf loads by putting it in a gtk image. What am I doing wrong here?

+1
source share
2 answers

I found out that I just need to force the function to call another exposure event with widget.queue_draw() at the end of the function. The function was called only once at the beginning, and at that moment there were no nodes, so nothing was drawn.

+2
source

You can use cairo for this. First, create a class based on gtk.DrawingArea, and attach the expose event to your expose function.

 class draw(gtk.gdk.DrawingArea): def __init__(self): self.connect('expose-event', self._do_expose) self.pixbuf = self.gen_pixbuf_from_file(PATH_TO_THE_FILE) def _do_expose(self, widget, event): cr = self.window.cairo_create() cr.set_operator(cairo.OPERATOR_SOURCE) cr.set_source_rgb(1,1,1) cr.paint() cr.set_source_pixbuf(self.pixbuf, 0, 0) cr.paint() 

This will draw an image every time an exposure event is fired.

+1
source

All Articles