[Tutor] Threads, ok to access parent script methods by passing 'self' ?

Walter Prins wprins at gmail.com
Tue Jun 5 12:41:17 CEST 2012


Hi Dave,

On 5 June 2012 11:13, dave selby <dave6502 at gmail.com> wrote:
> Hi All,
>
> If a thread is started and 'self' is passed as a parameter, is it
> acceptable to access methods of the calling class via
> 'self.updateGrid()' etc from within the thread or does everything have
> to be done via wx.lib.pubsub and publisher calls ?

I infer you're using WXPython?  Anyway, generally most GUIs run on a
single thread and the resources attached to these GUI objects can only
safely be accessed by that main thread that the GUI is running.
Directly calling on GUI objects from threads can somtimes work but can
give rise to very obscure and strange bugs.  Consequently you need to
use a mechanism of some sort to return data from or signal from a
worker thread back to the main thread, without directly calling the
GUI objects from these worker thread(s) (or directly calling methods
that directly call on GUI objects), which is what it sounds like your
updateGrid() method may be doing.

There are a number of ways to achieve this but concretely perhaps the
simplest way for now in your case is to use the wx.CallAfter() method.
 This basically lets you hand off the method to call together with the
parameters to use to WX, which will then cal your method on your
behalf, from the context of the main GUI thread.  See:
http://www.wxpython.org/docs/api/wx-module.html#CallAfter or
shortened: http://is.gd/abnhbG

HTH,

Walter


More information about the Tutor mailing list