drawing a pixbuf onto a drawing area using pygtk and glade
Asked Answered
S

2

4

i'm trying to make a GTK application in python where I can just draw a loaded image onto the screen where I click on it. The way I am trying to do this is by loading the image into a pixbuf file, and then drawing that pixbuf onto a 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 the pixbuf onto the image in the top left corner, but nothing shows but the white image. I have tested that the pixbuf loads by putting it into a gtk image. What am I doing wrong here?

Sop answered 22/4, 2009 at 2:34 Comment(0)
S
4

You can make use of cairo to do this. First, create a gtk.DrawingArea based class, and connect the expose-event to your expose func.

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 the image every time the expose-event is emited.

Shuster answered 23/5, 2009 at 3:41 Comment(0)
S
3

I found out I just need to get the function to call another expose event with widget.queue_draw() at the end of the function. The function was only being called once at the start, and there were no nodes available at this point so nothing was being drawn.

Sop answered 22/4, 2009 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.