“手动”在 gtk.DrawingArea 上调用公开事件

发布于 2024-12-24 18:47:12 字数 734 浏览 0 评论 0原文

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

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

问题是: 除了更新绘图区域之外,一切正常。我不知道如何手动调用公开。按 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())

I'm creating a small tool for data visualization.
The GUI is created with Pygtk and very simple. After the user has entered a filename he can press the button "calc" to see the output. The output is rendered by pycairo and presented in a gtk.DrawingArea.

The application works as following:
When the button is pressed the file is processed and its content is stored in a special class. This class is a custom container: It's instantiated together with the gui and either empty or filled. The DrawingArea's expose event is linked to a drawing function that reads the container and draws its content. As long as the container is empty the DrawingArea remains empty but after calc has been clicked and a file was loaded the DrawingArea should be filled with my visuals.

The problem is:
Everything works fine, except of updating the DrawingArea. I can't figure out how to manually invoke expose. After pressing calc I have to resize the window to see the result.

More specific:
I've copied this code from a tutorial and don't know how to provide the parameter event myself:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怕倦 2024-12-31 18:47:12

为了刷新您的小部件,您需要告诉 GTK+ 它需要重新绘制,通常使用 gtk.Widget.queue_draw()(称为 self.queue_draw()) >),这会使您的整个小部件无效并安排重绘(通过发出暴露事件),该重绘将在您的程序返回主循环后发生。

如果您需要对重绘区域进行更多控制,可以随时使用 gtk.Widget.queue_draw_area() 指定要无效的区域,或者一直到 gtk.gdk。 Window.invalidate_rect()。

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

习ぎ惯性依靠 2024-12-31 18:47:12

您已经编写了一个事件回调,需要将其连接到 expose 事件,如下所示:self.connect("do_expose_event", self.expose) >__init__ 对象的方法。

You've written a event callback, which you need to connect to the expose event like so:self.connect("do_expose_event", self.expose) in the __init__ method of your object.

青丝拂面 2024-12-31 18:47:12

从 calc 处理程序中调用 self.draw() 并忘记创建一个伪造的事件结构来传递给公开。

但是,如果您坚持通过直接调用来滥用 do_expose_event(),则无需创建整个事件结构,只需将其引用的一个元素 (event.area) 设置为窗口几何形状,以便没有任何内容被剪切。

Call self.draw() from the calc handler and forget about making a fake event structure to pass to expose.

But if you insist on misusing do_expose_event() by calling it directly, you don't need to create an entire event structure, just call it with the one element it references (event.area) set to the window geometry so that nothing gets clipped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文