PYQT5实现控制台显示功能
首先,写一个信号,用来发射标准输出作为信号
class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号 def write(self, text): self.textWritten.emit(str(text))
class Ui_MainWindow(QtWidgets.QMainWindow): def __init__(self): super(Ui_MainWindow,self).__init__() self.setupUi(self) self.retranslateUi(self) def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1131, 667) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.textEdit = QtWidgets.QTextEdit(self.scrollAreaWidgetContents_2) self.textEdit.setGeometry(QtCore.QRect(0, 0, 731, 301)) self.textEdit.setObjectName("textEdit") self.textEdit.setReadOnly(True) #下面将输出重定向到textEdit中 sys.stdout = EmittingStream(textWritten=self.outputWritten) sys.stderr = EmittingStream(textWritten=self.outputWritten) #接收信号str的信号槽 def outputWritten(self, text): cursor = self.textEdit.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(text) self.textEdit.setTextCursor(cursor) self.textEdit.ensureCursorVisible()