Python sleep doesn't work right in a loop?

Jeremy Bowers jerf at jerf.org
Wed Apr 6 18:44:00 EDT 2005


On Wed, 06 Apr 2005 12:49:51 -0700, ritterhaus wrote:

> Nope. Does't work. Running Python 2.3.4 on Debian, Linux kernel 2.6. This
> is actually test code for a larger project...
> 
> # flash the selected wx.TextControl
> 
> for flasher in range(4):
>     self.textField.SetBackgroundColour(255, 0, 0) time.sleep(0.8)
>     self.textField.SetBackgroundColour(255, 255, 223) time.sleep(0.8)
> 
> Even when I add an explicit call to repaint the TextCtrl between each
> sleep, things appear to be 'queued' until after the loop is fnished. Very
> bizarre.

GUIs don't like "time.sleep". All of them come with some sort of "fire a
timing event in X milliseconds and call this handler". Use that instead.

I believe wx's is the wxTimer class, and the wxFutureCall class looks
promising.

If you want to maintain the same basic calling structure, start playing
games with generators; you can write the same function with "yield", and
then call .next() on an iterator every time the timeout triggers. Best of
both worlds.




More information about the Python-list mailing list