I've implemented this answer into my code, which I was hoping would do what I want. However, I'm running a method via a connection on a QPushButton
, and I want to pipe what happens in this method to the GUI.
The first time I click the button the stdout
appears in the interpreter window; however, on subsequent presses of the button, the stdout
appears in the QTextEdit
- I assume there's some intricacy of the print statement, or of QPushButton
, that I don't understand - if anyone can give any pointers where I need to start changing my code I'll be eternally grateful!
I think this is the smallest amount of code I can use to demonstrate the problem..
import os, sys
from PyQt4 import QtCore, QtGui
def main():
app = QtGui.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
self.runBtn = QtGui.QPushButton('Run!', self)
self.runBtn.clicked.connect(self.runCmd)
self.te = QtGui.QTextEdit()
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.runBtn)
layout.addWidget(self.te)
self.setLayout(layout)
def runCmd(self):
print "here"
print sys.stdout
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def __del__(self):
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text):
cursor = self.te.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.te.setTextCursor(cursor)
self.te.ensureCursorVisible()
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
if __name__ == "__main__":
main()
You're mixing signals with method calls:
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
I'm not sure what this is supposed to do. You should do this instead:
self.stream = EmittingStream()
self.stream.textWritten.connect(self.normalOutputWritten)
but only once when you start the program. When you want to see the output, do this:
try:
sys.stdout = self.stream
... code to print something ...
finally:
sys.stdout = sys.__stdout__ # reset stdout to default
Aaron made a very good point, but my problem had a much simpler answer than the intricacies of object orientation in python...
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
needs to be after any print statements - print statements before this will be directed to the standard stdout
, ie the interpreter console.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.