[wxPython] Threads and MainLoop

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Threads and MainLoop

Mark Howson
Hi,

I'm having a little trouble with threads and wxPython. Some background
might help - I'm working on a large, moderately complex program that uses
Python and SWIG extensively.

What I'd really like to do is create a wxPython GUI (this is a separate
Python utility) that can run concurrently with my program.

I'm guessing this means running the wx GUI inside a thread, but so far this
hasn't quite worked. I should add I know *nothing* about threads, so if
this is a dumb mistake, please forgive me :)

I tried something like this:

import thread

class _myapp(wxApp):
  def __init__(self, parent, id, title):
    wxFrame.__init__(self, parent, -1, title, size = (600, 500))

    # Add
status bar
    self.status_bar = wxStaticText(self, -1, "Ready", wxPoint(5,
25),                               wxSize(75, 20), wxALIGN_CENTRE)

    < and so on>

app = _myapp(0)

def foo(self, args=None):
    app.MainLoop()

thread.Thread(target=foo).start()

< more code here >

This opens the GUI window okay, but the window is unresponsive. If I remove
the thread lines, it works beautifully, so I guess the problem isn't the
wxapp declaration. I've tried all sorts of combinations : inserting
time.sleep(1) in various places, using Pending()/Dispatch() instead, etc,
but so far no luck. Adding 'print' commands in the various threads suggest
they are being called okay (indeed, the demos work fine), but something is
wrong with the event handling.

Can anyone give me any clues as to where I might be going wrong?

I'm using RH6.0, Linux 2.2.13, wxPython 2.11/wxGTK 2.11 (both compiled from
source).

Mark




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Threads and MainLoop

Ken Seehof-2
It might be simpler to create a thread for the rest of your
program and use the main thread for the GUI.  That's the
usual approach.

Also, note that there are two thread related modules:

thread - low level threading functions
threading - Thread class and high level functions

thread.Thread doesn't exist.

Perhaps something like this is what you want:


import threading

class _myapp(wxApp):
  def __init__(self, parent, id, title):
    wxFrame.__init__(self, parent, -1, title, size = (600, 500))
    self.start_thread()

    # Add status bar
    self.status_bar = wxStaticText(self, -1, "Ready",
        wxPoint(5, 25), wxSize(75, 20), wxALIGN_CENTRE)

    < and so on>
    def start_thread(self):
        self.thread = threading.Thread(target=foo)
        self.thread.start()


app = _myapp(0)
app.MainLoop()


def foo(self, args=None):
    < more code here >


Mark Howson wrote:

> Hi,
>
> I'm having a little trouble with threads and wxPython. Some background
> might help - I'm working on a large, moderately complex program that uses
> Python and SWIG extensively.
>
> What I'd really like to do is create a wxPython GUI (this is a separate
> Python utility) that can run concurrently with my program.
>
> I'm guessing this means running the wx GUI inside a thread, but so far this
> hasn't quite worked. I should add I know *nothing* about threads, so if
> this is a dumb mistake, please forgive me :)
>
> I tried something like this:
>
> import thread
>
> class _myapp(wxApp):
>   def __init__(self, parent, id, title):
>     wxFrame.__init__(self, parent, -1, title, size = (600, 500))
>
>     # Add
> status bar
>     self.status_bar = wxStaticText(self, -1, "Ready", wxPoint(5,
> 25),                               wxSize(75, 20), wxALIGN_CENTRE)
>
>     < and so on>
>
> app = _myapp(0)
>
> def foo(self, args=None):
>     app.MainLoop()
>
> thread.Thread(target=foo).start()
>
> < more code here >
>
> This opens the GUI window okay, but the window is unresponsive. If I remove
> the thread lines, it works beautifully, so I guess the problem isn't the
> wxapp declaration. I've tried all sorts of combinations : inserting
> time.sleep(1) in various places, using Pending()/Dispatch() instead, etc,
> but so far no luck. Adding 'print' commands in the various threads suggest
> they are being called okay (indeed, the demos work fine), but something is
> wrong with the event handling.
>
> Can anyone give me any clues as to where I might be going wrong?
>
> I'm using RH6.0, Linux 2.2.13, wxPython 2.11/wxGTK 2.11 (both compiled from
> source).
>
> Mark
>
> _______________________________________________
> wxPython-users maillist  -  [hidden email]
> http://starship.python.net/mailman/listinfo/wxpython-users




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users