如何拦截 GTK 主循环,或者使用 python gtk 为主函数提供回调事件?
我有一个显示滑块的基本程序。我理解它们使用基于事件的回调的概念。我想做的是更新图像,或者自动更新日志,而不需要用户交互,所以它基本上会一直调用这个事件。我不确定是否有回调,但如果有任何建议,我们将不胜感激。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您可以使用 < code>gobject.timeout_add 来执行此操作。
If I'm understanding you right, you can use
gobject.timeout_add
to do this.这看起来像轮询,这很糟糕,因为它消耗资源。事实上,您仍然应该使用事件,因为当您的图像发生更改时,应该触发应该触发更新的事件。
但是,如果您想在后台执行某些操作,也许您想要的是一个空闲事件处理程序,该处理程序在每次程序没有更多事件要处理时运行。使用
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), org_timeout_add
for milliseconds granularity.