[Tutor] time.sleep?

Jeff Shannon jeff at ccvcorp.com
Thu Dec 2 02:51:10 CET 2004


Christian Wyglendowski wrote:

>>-----Original Message-----
>>[...] So nothing actually 
>>appears until the for loop and sleep are finished. 
> 
> I think that is because wxPython is waiting for the function to exit
> before updating the GUI.  Since your OnGoButton() function sleeps for
> some amount of time, it will take a while before the GUI gets updated.  

Yep, exactly.

The key here is that, when using a GUI or other event-driven 
framework, you need to allow the framework some execution time too. 
What has happened is that you've told the framework to add a message 
to the text field, but the framework doesn't update the screen until 
*after* you're done handling all the events in your event queue. 
Obviously, if you're still in the same event handler, you haven't 
finished handling all events... ;)

Given that you're (apparently) using wxPython, you should look into 
using wx.Timer.  In essence, instead of sleeping in a loop and then 
updating the message every X seconds, your button handler will simply 
toggle a flag variable saying "I'm running".  You will also have a 
handler for the timer's event, and in that handler, you check the flag 
variable.  If the flag is set, then you check the file and update the 
message as needed.  If the flag is *not* set, then you do nothing. 
(It's also possible to start and stop the timer events from happening 
in the first place, rather than just ignoring them if the flag isn't 
set.  But I don't remember how to do that off the top of my head, so 
I'm not going into detail here. ;) )

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list