2009-04-22 134 views
1

我試圖在python中創建一個GTK應用程序,我只需在屏幕上繪製加載的圖像,然後點擊它。我試圖這樣做的方法是將圖像加載到pixbuf文件中,然後將該pixbuf繪製到繪圖區域。使用pygtk和glade將繪圖區域繪製到繪圖區域

代碼的主線是在這裏:

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) 

這應該只是繪製的pixbuf到圖像的左上角,但沒有顯示,但白色圖像。我已經通過將它放入gtk圖像來測試pixbuf加載。我在這裏做錯了什麼?

回答

2

我發現我只是需要調用另一個揭露事件的函數widget.queue_draw()函數結束。該函數在開始時只被調用一次,並且此時沒有節點可用,因此沒有繪製任何節點。

1

你可以使用開羅做到這一點。首先,創建一個基於gtk.DrawingArea的類,並將expose-event連接到你的公開函數。

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() 

這會在每次暴露事件發生時繪製圖像。