异步更新 PyGTK 托盘图标
正如此处所解释的,我有一个使用 PyGTK 的简单托盘图标。
作为 GTK 的新手,在我看来 gtk.main() 是同步的,会阻止任何进一步的处理,直到相应的 UI 关闭。
那么我如何定期(例如每 5 秒)更新/刷新 StatusIcon
的图标 - 我是否必须求助于 Twisted 等人?为了这?
As explained here, I have a simple tray icon using PyGTK.
Being very new to GTK, it appears to me that gtk.main()
is synchronous, blocking any further processing until the respective UI is closed.
So how can I periodically (e.g. every 5 seconds) update/refresh StatusIcon
's icon - do I have to resort to Twisted et al. for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
gobject.timeout_add()
添加周期性任务。第一个参数是以秒为单位的间隔,第二个参数是您要调用的回调。只要回调在用作bool
时返回计算结果为True
的内容,就会被调用。另请参阅 PyGTK 常见问题解答的第 20 节,特别是 问题 20.7。
You can use
gobject.timeout_add()
to add periodical tasks. The first parameter is the interval in seconds, the second parameter is the callback you want to be called. The callback is called as long as it returns something that evaluates toTrue
when used as abool
.See also section 20 of the PyGTK FAQ, in particular question 20.7.
您无法轻松刷新托盘图标,而是从已经尝试过此操作但失败的某个人那里获取它(实际上我只是放弃它,因为“如果实现很难解释,这是一个坏主意。< /strong>") ,
但这是我到目前为止所拥有的,要刷新状态图标,您必须使用 doc :
因此,如果您想刷新状态图标,您必须使用 gio 主题创建图标icon ,现在您可以直接更新主题图标,此更改将直接显示在托盘状态中。
现在关于您的问题:
是的 gtk.main() 阻止等待信号,因此您只需将信号绑定到操作,当触发此信号时,可以执行操作,对于您的操作回调中的情况,您可以将将刷新您的状态图像的代码。
希望我在这里对你有所帮助。
You can't refresh easily the tray icon , take it from some one that did already try this and failed (Actually i just drop it for the reason that "If the implementation is hard to explain, it's a bad idea.") ,
but here is what i have so far, to refresh the Status Icon you will have to set your status icon using
gtk.status_icon_new_from_gicon()
method, from the doc :So if you want to refresh the Status Icon you will have to create your icon using gio themed icon , and now you can update directly your themed icon and this change will be shown directly in the tray status.
Now about your question :
Yes gtk.main() block waiting for signal, so you just have to bind a signal to an action and when this signal is triggered the action can be executed , for your case in your action callback you can put the code that will refresh your Status Image.
Hope i did help you here.