[Tutor] while loops causing python.exe to crash on windows

Alan Gauld alan.gauld at btinternet.com
Mon Jun 7 09:59:17 CEST 2010

"Alex Hall" <mehgcap at gmail.com> wrote

> I am not sure how else to explain it. I want to loop until the value
> of a variable changes, but while that loop is taking place, the user
> should be able to perform actions set up in a wx.AcceleratorTable.

And here we have the critical clue. You are trying to write this
loop in a GUI application. GUIs and long running loops don't
mix. GUIs are driven by events and if you write a long loop
the GUI cannot receive any events until the loop finishes
and so "locks up"

In a GUI environment you simulate a long loop with
1) A Timer event that does someting then sets up another timer
2) The null event (if the framework suipports it) whereby
when no other events are present the framework calls your code.

> for a "listener", which will sit in the background and only perform 
> an
> action when it detects a certain thing. In this case, a listener to
> watch for a variable to turn from False to True, then to act when it
> sees that change.

The listener sits inside a Timer that fires every, say, 1/10 sec.
That should leave plenty time to respond to the variable change
and give wx time to process any mouse clicks, key presses etc.

BTW. If you are doing any significant GUI work in wxPython
it will be helpful to buy the wxPython in Action book. It is very
clear and includes examples of using Timers etc.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list