How do I Block Events in wxPython

Frank Millman frank at chagford.com
Thu Dec 10 09:01:28 EST 2009


Stephen Hansen wrote:

> On Wed, Dec 9, 2009 at 10:21 PM, Frank Millman <frank at chagford.com> wrote:
>
>> I also need to block events in my wxPython app, though the time duration 
>> is
>> very short. I have a separate thread that sends notification of gui 
>> events
>> to a server, and waits for a response. I do not want the user to do
>> anything until the response is received.
>>
> I don't think "blocking" events is the right way to think about this.
>
> If you need to halt new input for some reason (bearing in mind that its 
> best
> to run such things in a background task, but yes, sometimes blocking the 
> UI
> is important), then there's a couple ways to go about it. But trying to 
> mess
> with the event loop isn't it.

In fact my method does not work. All that happens is that the events are 
queued, and as soon as I release the lock the events are processed in a 
bunch. Not clever :-(

>
> First, you always want a visual indicator-- use SetCursor on your top 
> level
> window to set a busy cursor. You never want any sort of block-ish action 
> to
> happen without the user being able to see "something is going on"; if its
> more then a second or so I /really/ think you should throw up a progress
> indicator dialog, even if its an infinate one.
>

Agreed.

> To actually 'block' the events themselves, you can just call
> wnd.Enable(False). Just be sure to Enable(True) when you want to process
> stuff again.

This may work for the OP, but would not really work for me, because it 
changes the visual appearance of all the controls. In my case the time 
duration for blocking is usually very short, so it could result in flicker.

>
> Another approach is to use wnd.CaptureMouse() on a particular control 
> which
> doesn't really respond to anything. Just be sure to ReleaseMouse() later 
> and
> follow the instructions in the docs about capturing that cancel-capture
> event.
>

I like this. Unfortunately it does not block keyboard input. However, I have 
a keyboard event handler on virtually all my controls, so it should be easy 
to set a flag and tell it to ignore keystrokes while in a 'blocked' state.

> HTH,

It certainly does - thanks.

Frank






More information about the Python-list mailing list