[wxPython-users] calling wxPython functions from different thread

Robin Dunn robin at alldunn.com
Mon Feb 11 12:54:51 PST 2008


Jacek Poplawski wrote:
> hello again,
> 
> I was trying to make my application multithreaded, I put my calculations 
> in different Thread, then I gave this Thread name of the function to 
> call when it finish.
> This function updates ListCtrl.
> So it works this way:
> 
> - ListCtrl has old values
> - I want to add new element, it requires calculations
> - I call different thread and give it reference to Update() function
> - I do different things while thread does its calculations
> - thread finishes calculations and calls Update()
> - Update() regenerates data in ListCtrl
> 
> The problem is, that my GUI is unstable after last step. ListCtrl is not 
> updated until I use scroll, then it updates, but I can't click anything 
> else in the wxPython window.
> 
> Can I use threads this way when developing wxPython application?

Yes, but you can't call any function directly from the thread that will 
interact with the gui.  See http://wiki.wxpython.org/LongRunningTasks 
for more ideas, but the easiest is to just group what you want to do 
with the GUI into a separate function, and then use wx.CallAfter from 
your thread to call that function.  Under the covers it will make an 
event containing a reference to the function you want called and the 
parameters you want to pass to it, and then send that event to the 
wx.App object.  The wx.App object will then process that event in the 
context of the gui thread and will call the function for you.

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list