This is probably very basic, but it’s giving me a headache, and I’m not sure what method to even approach it with, making the googling tough.
If I have a class in a module that I’m importing with various prints throughout, how can I read the prints as they come so that I may output them to a PyQT text label?
class Worker(QtCore.QThread, object):
class statusWrapper(object):
def __init__(self, outwidget):
self.widget = outwidget
def write(self, s):
self.widget.setText(s)
def __init__(self, widget):
QtCore.QThread.__init__(self)
sys.stdout = statusWrapper(widget)
def run(self):
self.runModule() #This is the module with the prints within.
Something mysterious gets passed to the statusWrapper.write when runModule gets executed, but it’s blank. What am I doing wrong?
Thanks.
Instead of writing your own statusWrapper class, you could use a StringIO object as stdout. Something like:
Restoring the original value of stdout is important for your sanity. Also note that this will not do what you expect in a multithreaded environment. Also note that delnan is certainly correct, and replacing stdout is an incredibly hackish way of doing this.
If you’re wanting something which will update the label each time the module prints output (sort of a poor man’s status indicator), there are better ways to do that too – you could replace the prints with calls to a callback function, which you set in the module when you import it, or something like that.