使用pygtk和glade将pixbuf绘制到绘图区域

时间:2009-04-22 02:34:43

标签: python drawing pygtk glade

我正在尝试在python中创建一个GTK应用程序,我可以将加载的图像绘制到我点击它的屏幕上。我试图这样做的方法是将图像加载到pixbuf文件中,然后将pixbuf绘制到绘图区域。

主要代码在这里:

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绘制到左上角的图像上,但没有显示白色图像。我测试了pixbuf加载到gtk图像加载。我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

我发现我只需要在函数末尾使用widget.queue_draw()来调用另一个公开事件的函数。该函数仅在开始时被调用一次,此时没有可用的节点,因此没有任何东西被绘制。

答案 1 :(得分:2)

您可以使用cairo来执行此操作。首先,创建一个基于gtk.DrawingArea的类,并将expose-event连接到您的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()

这将在每次发出expose事件时绘制图像。