[Tutor] program hangs in while loop using wx.yield

Alan Gauld alan.gauld at btinternet.com
Mon Nov 15 09:20:56 CET 2010


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

> Is there a basic tutorial for this sort of thing?

There are a few books on the suvbject but I can't thik of
any titles off the top of my head. And mostly they are
written for C++ rather than Python.

> fraction of a second. However, I need to wait for as long as the 
> human
> takes to make a turn-ending move. Firing ends a turn, while moving 
> the
> mouse or arrows does not.

Thats OK you just wait till you get a Fire event. Presumably
you have a dedicated "Fire" Button or somesuch?

> The computer does not have this problem, but
> I still need to know when the computer is done.

What you really need is an indication on the GUI so that
the player knows when the computer is done? All of the
processing of the computers move will be part of the "Fire"
button event handler.

> You are right, that was confusing. What I am saying is that, each 
> time
> a player goes (human or ai) a string gets updated. The opponent can
> then examine that string to see what happened, updating the gui (in
> the case of the human) accordingly and recording the information for
> consideration in future moves (in the case of the ai).

You might find it helpful to write down the system requirements
as a set of "use cases". A use  case is a dialog between user
and system. So your description above would look something like:

1) user clicks cell on system
2) system updates display to indicate hit or miss
3) system displays target cell on GUI
4) system displays whether this is a hit or miss
5) continue at 1

In this example actions 2,3 and 4 can all be part of the same
event handler. Or you could break it into two actions separated
by a timer event. To prevent the user doing anything before the
timer expires simply set a semaphore or disable the UI control.

GUI design often uses a state machine, see the recent thread
on state variables for more info. Keeping the integrity of the
state machine is a large part of successful GUI design.
In complex GUIS I often include a setState function (that
gets called after every event) which simply sets the enabled/disabled
properties for all the controls depending on the current state.
This is usually a big table of states versus controls and I retrieve
the two lists of enabled and disabled controls and iterate over them
setting as appropriate...

>> Why do you care if the human "takes in the results" - if they want
>> to trust in randomness surely thats their problem. You as a 
>> computer
>> can match them blast for blast...
> Part of it is that I hope to eventually add sound, and the other 
> part
> is that I have always found games that make a move instantly to be a
> bit too far from what I am used to. If it is easy to build in a 
> delay,
> I would rather have it there.

Delays are easier as per my earlier message. Either just add
a sleep(1) to your code or use a timer. I'd suggest the latter,
its more work but it is more flexible and keeps the GUI responsive
where it needs to be (menus, moving, resizeing, etc)

In your case I'd do step 2 first then set a 1 second timer before
doing steps 3 and 4. Disable the fire capability in step 2 and
re-enable it after step 4.

> Again: is there a basic tutorial that would explain how control 
> works
> when dealing with a gui, especially one that is meant to last a long
> time but occasionally block input?

You probably can find one on GUI design or event driven programming
but the best advice I can offer is to think about the dialog/use case 
approach.
Every time the user does something you have to think about how
the system will respond and map that to an event handler.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list