问题
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