Question

I'm trying to write a WiFi management interface using Python and PyGTK.

The user sees a list of available networks in a gtk.TreeView. S/he then has an option to rescan the wireless networks by clicking a button. For whatever reason, the rescanning process is really quick. So quick in fact, that it feels like nothing has happened on clicking the button, even though the list of networks is indeed being rescanned.

What I'd like to do is to add a couple of seconds of delay, where the interface blocks and I display a gtk.Spinner so that the user sees that the gtk.TreeView I use to display the list of networks is cleared and repopulated.

Obviously, time.sleep() isn't the right way. Is there an elegant way of doing this?

Was it helpful?

Solution

Regarding LOC the shortest answer i could come up with:

from threading import Thread
import gobject

gobject.threads_init()

time_to_wait = 2.0      # time in seconds
nonblocking_wait = Thread(target=sleep, args=(timetowait,))
nonblocking_wait.start()

while nonblocking_wait.is_alive():
    nonblocking_wait.join(.05)
    gtk.main_iteration_do(True)     # blocks if no event is pending

On a second glance i realized, that for some readers the relevance of the sleep-thread might be obscure. It's purpose is to establish a maximum timelimit of time_to_wait + 0.05 seconds (+ overhead)! you could do it with a counter or similar approaches, but this way you prevent an useless load on the cpu in idle loops.

This way gtk can still handle draw-updates.

any progressbar-updates or whatever must be placed into the while loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top