如何拦截 GTK 主循环,或者使用 python gtk 为主函数提供回调事件?

发布于 2025-01-03 15:46:51 字数 110 浏览 3 评论 0原文

我有一个显示滑块的基本程序。我理解它们使用基于事件的回调的概念。我想做的是更新图像,或者自动更新日志,而不需要用户交互,所以它基本上会一直调用这个事件。我不确定是否有回调,但如果有任何建议,我们将不胜感激。

I have a basic program working that displays a slider. I understand the concept of them using callbacks based on events. What I want to do is say update an image, or update the log automatically without having a user interact, so it would basically be calling this event all the time. I'm not sure if there is a callback for it or not but any suggestions would be appreciated.

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

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

发布评论

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

评论(2

凉墨 2025-01-10 15:46:51

如果我理解正确,您可以使用 < code>gobject.timeout_add 来执行此操作。

If I'm understanding you right, you can use gobject.timeout_add to do this.

情深缘浅 2025-01-10 15:46:51

这看起来像轮询,这很糟糕,因为它消耗资源。事实上,您仍然应该使用事件,因为当您的图像发生更改时,应该触发应该触发更新的事件。

但是,如果您想在后台执行某些操作,也许您想要的是一个空闲事件处理程序,该处理程序在每次程序没有更多事件要处理时运行。使用g_idle_add为此。

如果您想要执行一个操作,例如每 5 秒执行一次,那么就按照 Jeremy 的建议,使用计时器事件。您应该使用 g_timeout_add_seconds 为一秒的倍数(它可以避免无缘无故地唤醒CPU,这对于良好的电源管理很有用),或g_timeout_add< /a> 为毫秒粒度。

This looks like polling, which is bad, as it's ressource consuming. In fact you should still use events, as the event that should trigger the update should be fired when your image has changed.

If you want to execute stuff in the background however, maybe what you want is an idle event handler, which is run each time the program has no more event to process. Use g_idle_add for that.

If what you want is to perform an action, say, every 5 seconds, then used a timer event instead, as Jeremy proposed. You should use g_timeout_add_seconds for a time multiple of a second (it avoids waking up the CPU for no reason, which is useful for good power management), or g_timeout_add for milliseconds granularity.

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