Asynchronously updating PyGTK tray icon

假装没事ソ 提交于 2020-02-05 05:57:25

问题


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?


回答1:


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 to True when used as a bool.

See also section 20 of the PyGTK FAQ, in particular question 20.7.




回答2:


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 :

Creates a status icon displaying a gio.Icon(). If the icon is a themed icon, it will be updated when the theme changes.

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.



来源:https://stackoverflow.com/questions/4708636/asynchronously-updating-pygtk-tray-icon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!