Note that there are some explanatory texts on larger screens.

plurals
  1. POwxPython, Threads, and PostEvent between modules
    primarykey
    data
    text
    <p>I'm relatively new to wxPython (but not Python itself), so forgive me if I've missed something here.</p> <p>I'm writing a GUI application, which at a very basic level consists of "Start" and "Stop" buttons that start and stop a thread. This thread is an infinite loop, which only ends when the thread is stopped. The loop generates messages, which at the moment are just output using <code>print</code>. </p> <p>The GUI class and the infinite loop (using <code>threading.Thread</code> as a subclass) are held in separate files. What is the best way to get the thread to push an update to something like a <code>TextCtrl</code> in the GUI? I've been playing around with <code>PostEvent</code> and <code>Queue</code>, but without much luck.</p> <p>Here's some bare bones code, with portions removed to keep it concise:</p> <p><b>main_frame.py</b></p> <pre><code>import wx from loop import Loop class MainFrame(wx.Frame): def __init__(self, parent, title): # Initialise and show GUI # Add two buttons, btnStart and btnStop # Bind the two buttons to the following two methods self.threads = [] def onStart(self): x = Loop() x.start() self.threads.append(x) def onStop(self): for t in self.threads: t.stop() </code></pre> <p><b>loop.py</b></p> <pre><code>class Loop(threading.Thread): def __init__(self): self._stop = threading.Event() def run(self): while not self._stop.isSet(): print datetime.date.today() def stop(self): self._stop.set() </code></pre> <p>I did, at one point, have it working by having the classes in the same file by using <code>wx.lib.newevent.NewEvent()</code> along <a href="http://wiki.wxpython.org/index.cgi/AsynchronousSockets" rel="nofollow noreferrer">these lines</a>. If anyone could point me in the right direction, that'd be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload