wxPython: non-GUI thread launching new frame? Delegates?

Chris Mellon arkanes at gmail.com
Tue Feb 20 10:26:24 EST 2007


On 2/20/07, Diez B. Roggisch <deets at nospam.web.de> wrote:
> cyberco wrote:
>
> > In my wxPython app a non-GUI thread (that reads info from the network)
> > tries to open a frame to show the new info. This results in my app
> > hanging (which is not too surprising). Coming from a C# environment I
> > wonder if there is some sort of delegate mechanism in wxPython to do
> > this sort of thing.
>
> Not sure how wx deals with this, but one thing you might explore is the
> possibility to add a timer in the GUI-thread, that polls a thread-filled
> queue.
>
> Other toolkits as Qt have means to insert an extra event in the event queue
> of the gui-thread in a thread-agnostic way, maybe wx has that too.
>
> Googling...
> ...
> ...
> ...
>
> ... finished
>
> http://mail.python.org/pipermail/python-list/2005-August/335467.html
>
> """
> You need another way to pass completion information between the downloader
> thread and the main thread; the simplest way is to define a custom wx
> Event, and wxPostEvent from the downloader thread when it completes (
> and when the gauge should be updated).  wxPostEvent is safe to call from
> non-eventloop threads.
> The main thread's wx event loop just spins, properly updating all other
> parts of the GUI, and receiving events from the downloader thread.
>
> ANother approach is to have a thread-safe Queue and have the main
> thread/event loop
> poll the queue with queue.get_nowait() periodically (typically 0.1-1 sec).
> The downloader thread shares the queue object and puts data structures
> (typically
> class instances, strings, or ints) that indicate status updates.
> """
>
> So - both options a viable. And read to the end, the twisted-approach
> certainly is the most clean one.
>

This is rather out of date. wxPython provides a wx.CallAfter function,
which will call the passed callable on the next spin through the event
loop. It's essentially a wrapper around the custom event mechanism
described above.

> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list