4

我正在创建一个用于数据可视化的小工具。GUI 是用 Pygtk 创建的,非常简单。用户输入文件名后,他可以按“calc”按钮查看输出。输出由 pycairo 渲染并呈现在 gtk.DrawingArea 中。

该应用程序的工作方式如下:当按下按钮时,文件被处理,其内容存储在一个特殊的类中。此类是一个自定义容器:它与 gui 一起实例化,并且为空或填充。DrawingArea 的暴露事件链接到一个绘图函数,该函数读取容器并绘制其内容。只要容器为空,DrawingArea 就保持为空,但在单击 calc 并加载文件后,DrawingArea 应该填充我的视觉效果。

问题是:一切正常,除了更新绘图区。我不知道如何手动调用暴露。按下 calc 后,我必须调整窗口大小才能看到结果。

更具体:我从教程中复制了这段代码,不知道如何自己提供参数事件:

def do_expose_event(self,event):
    # Create the cairo context
    cr = self.window.cairo_create()

    # Restrict Cairo to the exposed area; avoid extra work
    cr.rectangle(event.area.x, event.area.y,
            event.area.width, event.area.height)
    cr.clip()

    self.draw(cr, *self.window.get_size())
4

3 回答 3

13

In order to have your widget refreshed, you need to tell GTK+ that it needs to be redrawn, typically using gtk.Widget.queue_draw() (called as self.queue_draw()), which invalidates your entire widget and schedules a redraw (by emitting expose events) that will take place after your program returns to the main loop.

If you need more control over the region being redrawn, you can always use gtk.Widget.queue_draw_area() to specify an area to invalidate, or go all the way down to gtk.gdk.Window.invalidate_rect().

于 2012-01-06T01:31:51.810 回答
0

您已经编写了一个事件回调,您需要expose像这样连接到事件:self.connect("do_expose_event", self.expose)__init__您的对象的方法中。

于 2012-01-05T16:05:44.313 回答
0

从 calc 处理程序调用self.draw()并忘记制作一个假事件结构以传递给公开。

但是如果你坚持do_expose_event()直接调用它来滥用,你不需要创建一个完整的事件结构,只需将它引用的一个元素 (event.area) 设置为窗口几何图形来调用它,这样什么都不会被剪裁。

于 2012-01-05T19:57:52.280 回答