| FAQ Index - Search - Recent Changes - Everything - Add entry |
23.37. How do I create a throbber like the one in Firefox?
Easy, steal it from Firefox! Download the Firefox source and find the
files Throbber.png and Throbber.gif (or do a Google Images search). Throbber.png is the static icon and Throbber.gif is the animated icon. Pack a gtk.Image with the static icon next to your menubar:
hbox = gtk.HBox(expand=False)
hbox.pack_start(menubar)
self.throbber = gtk.Image()
self.throbber.set_from_file('image_path/Throbber.png')
hbox.pack_start(self.throbber, expand=False)
and create the animation:
self.animation = gtk.gdk.PixbufAnimation('image_path/Throbber.gif')
Then all we need to do to get the throbber throbbing is load this animation into the Image:
pixbuf = self.throbber.get_pixbuf() # save the Image contents so we can set it back later self.throbber.set_from_animation(self.animation) ... # then later to stop the animation just put the old contents back self.throbber.set_from_pixbuf(pixbuf)Here's an example of using the throbber to show activity while we do some hard work in a thread:
gobject.threads_init() # called somewhere at the top of your file
def someActionCallback(self, *args):
pixbuf = self.throbber.get_pixbuf()
self.throbber.set_from_animation(self.animation)
# make sure the throbbing starts now
while gtk.events_pending():
gtk.main_iteration()
def call():
try:
# hard working code goes here
finally: # make sure the throbbing stops whatever else happens
gobject.idle_add(self.throbber.set_from_pixbuf, pixbuf)
# start the hard work in a thread
threading.Thread(target=call).start()
(See section 20 of this FAQ for more on using threads in PyGtk.)
