我正在尝试在 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 图像来测试它是否加载。我在这里做错了什么?
最佳答案
您可以使用 cairo 来执行此操作。首先,创建一个基于 gtk.DrawingArea 的类,并将公开事件连接到您的公开函数。
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-event 时绘制图像。
关于python - 使用 pygtk 和 glade 将 pixbuf 绘制到绘图区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/775528/