Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Something mysterious gets passed to the statusWrapper.write when runModule gets executed, but it's blank. What am I doing wrong?</p> </blockquote> <p>It's nothing mysterious: <code>write</code> just receives each string that was written to <code>sys.stdout</code> (that is, your wrapper, in this case).</p> <p>The bug is probably that the wrapper calls <code>setText</code> only, <em>replacing</em> the widget's text on each write, instead of appending to it. You'll need to at least do something like:</p> <pre><code> def write(self, s): self.widget.setText(self.widget.text() + s) </code></pre> <p>(or whatever the more efficient way is of appending text to a QT widget).</p> <h2>Note:</h2> <p>A much better way to redirect <code>sys.stdout</code> is to use a context manager. <a href="http://www.python.org/dev/peps/pep-0343/" rel="nofollow" title="PEP 343 -- The &quot;with&quot; Statement">PEP 343</a> defines the following example:</p> <pre><code>from contextlib import contextmanager @contextmanager def stdout_redirected(new_stdout): save_stdout = sys.stdout sys.stdout = new_stdout try: yield None finally: sys.stdout = save_stdout </code></pre> <p>You would use it like:</p> <pre><code>class Worker(QtCore.QThread, object): def run(self): with stdout_redirected(StatusWrapper(widget)): self.runModule() </code></pre> <p>Besides being more readable, this context manager makes sure to restore sys.stdout if <code>runModule</code> raises an exception (which is important for your sanity :-).</p>
 

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