threads and exception in wxPython

Josiah Carlson jcarlson at uci.edu
Wed Nov 3 19:27:09 EST 2004


Jeff Shannon <jeff at ccvcorp.com> wrote:
> 
> Josiah Carlson wrote:
> 
> >Zunbeltz Izaola <zunbeltz at wm.lc.ehu.es.XXX> wrote:
> >  
> >
> >>If I put some code after the .start() method the queue will be
> >>empty. Do I need to insert a infinite loop to see when the exception
> >>is raised?
> >>    
> >>
> >
> >In your main thread (the one handling the GUI), you can set up a
> >wx.Timer() whose bound event checks the queue every second or so.
> >
> >As another poster mentioned, you can also post events to the GUI, which
> >can either say "there is an exception traceback in the queue" or "here
> >is the exception traceback".
> >  
> >
> 
> Another possibility is to check the queue in an idle-event handler.  
> When you place something in the queue, you can call wx.WakeUpIdle() to 
> ensure that the main thread will do idle processing at its soonest 
> opportunity.

The only ugly part about idle handling is that idle handlers are called
quite often (at least in the wxPython versions I've run).  Specifically,
whenever I move my mouse over a wxPython app window, idle events are
streamed to the idle event handler at 30-60 events/second.

Not really a big deal, but something people should know about none the
less.


 - Josiah

#source I used, lack of new namespace the result of copy/paste from
#wxPython wiki

from wxPython.wx import wxPySimpleApp, wxFrame, EVT_IDLE
import time

class myframe(wxFrame):
    def __init__(self, *args, **kwargs):
        wxFrame.__init__(self, *args, **kwargs)
        self.t = None
        EVT_IDLE(self, self.idle_handler)
    
    def idle_handler(self, evt):
        if self.t is None:
            self.t = time.time()
        else:
            print round(time.time()-self.t, 3),
            self.t = time.time()
        evt.Skip()

app = wxPySimpleApp()
frame = myframe(None, -1, "Ugly Idle Handling")
frame.Show(1)
app.MainLoop()


#output on win2k, Python 2.3.2, wxPython 2.4.2.4  while wiggling my
#mouse over the window...

C:\Temp>test_idle.py
1.828 0.016 0.015 0.032 0.015 0.016 0.015 0.032 0.031 0.016 0.406 0.547 0.031 0.
016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.01
6 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.015
0.016 0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.
015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.016 0.01
6 0.016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016
0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.015 0.
016 0.015 0.016 0.016 0.015 0.016 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.01
6 0.015 0.016 0.015 0.016 0.016 0.015 0.016 0.016 0.015 0.016 0.015 0.016

C:\Temp>




More information about the Python-list mailing list