title: Capturing stdout from an imported module in wxpython and sending it to a textctrl, without blocking the GUI
body: <p>There are alot of very similar questions to this but I can't find one that applies specifically to what I'm trying to do.</p>
  
  <p>I have a simulation (written in SimPy) that I'm writing a GUI for, the main output of the simulation is text - to the console from 'print' statements. Now, I thought the simplest way would be to create a seperate module GUI.py, and import my simulation program into it:</p>
  
  <pre><code>import osi_model
  </code></pre>
  
  <p>I want all the print statements to be captured by the GUI and appear inside a Textctrl, which there's countless examples of on here, along these lines:</p>
  
  <pre><code>class MyFrame(wx.Frame):
      def __init__(self, *args, **kwds):
          &lt;general frame initialisation stuff..&gt;
  
          redir=RedirectText(self.txtCtrl_1)
          sys.stdout=redir
  
  class RedirectText:
      def __init__(self,aWxTextCtrl):
          self.out=aWxTextCtrl
  
      def write(self,string):
          self.out.WriteText(string)
  </code></pre>
  
  <p>I am also starting my simulation from a 'Go' button:</p>
  
  <pre><code>def go_btn_click(self, event):
      print 'GO'
      self.RT = threading.Thread(target=osi_model.RunThis())
      self.RT.start()
  </code></pre>
  
  <p>This all works fine, and the output from the simulation module is captured by the TextCtrl, except the GUI locks up and becomes unresponsive - I still need it to be accessible (at the very minimum to have a 'Stop' button). I'm not sure if this is a botched attempt at creating a new thread that I've done here, but I assume a new thread will be needed at some stage in this process.</p>
  
  <p>People suggest using wx.CallAfter, but I'm not sure how to go about this considering the imported module doesn't know about wx, and also I can't realistically go through the entire simulation architecture and change all the print statements to wx.CallAfter, and any attempt to capture the shell from inside the imported simulation program leads to the program crashing.</p>
  
  <p>Does anybody have any ideas about how I can best achieve this? So all I really need is for all console text to be captured by a TextCtrl while the GUI remains responsive, and all text is solely coming from an imported module.</p>
  
  <p>(Also, secondary question regarding a Stop button - is it bad form to just kill the simulation thread?). </p>
  
  <p>Thanks,</p>
  
  <p>Duncan</p>