wxPython update problem

David Bolen db3l at fitlinxx.com
Mon Apr 7 18:12:36 EDT 2003


"Nils Grimsmo" <nilsgri at idi.ntnu.no> writes:

> i have a problem getting wxPython to update the gui while the thread is
> running user procedures. i have tried calling wxWindow.Refresh() and
> wxWindow.Update() in various orders both on the wxFrame and the wxButton, 
> but the gui doesn't update.

The only time that the GUI will be updated is when events are being
processed, which is normally blocked while you are processing a
current event.  You can use wxYield() to process any pending events
while still inside a current event handler if necessary to get GUI
updates without returning from your current event handler.

If you don't yield (or return from processing the current event), the
exact behavior may be platform and/or version dependent, as some
widgets on some platforms may immediately update some of their
attributes when they are changed, while others may just wait for an
eventual paint event to do so.

> i have posted source for a test program below. what i would like it to do,
> is to update the button with the new label before control is returned from
> run().
> (...)

When you have your time.sleep(1.0) you are sleeping, but doing so in
an event handler such that wxPython does not continue to process any
events.  If instead you added a call to wxYield() after the update,
you should consistently see the change.

Normally, this sort of thing shouldn't be necessary, because as soon
as your run() returns, events will be processed and the button
updated.  So I'm assuming that in your larger application, run() is
actually going to run a lengthy process which may need the GUI updated
along the way.  In such a case there are a few general approaches you
can take (including putting your processing into a separate thread so
the GUI thread remains responsive).  You might want to check out
http://wiki.wxpython.org/index.cgi/LongRunningTasks to see if it
provides a little more insight into approaches you might take.

> another thing i am wondering about is to disable the button
> temporarily. must i use wxEvtHandler::Disconnect?

I suppose you could, but the more typical way would be to just disable
the button window itself (with the Enable(False) method), which should
"grey out" the button and no longer respond to any mouse clicks.

Of course, there's still some potential that someone might manage to
click on the button twice before your code runs and disables it (this
is also true with the disconnect option), so it's not a bad idea to
sanity check your run() operation so that it only starts up a process
if one isn't already running, if that is one of your constraints.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list