实时追加显示
-
code
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import * from PyQt5.QtWidgets import * import time import sys class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号 def write(self, data): self.textWritten.emit(str(data)) class BackendThread(QThread): # 通过类成员对象定义信号 update_date = pyqtSignal(str) # 处理业务逻辑 def run(self): while True: data = QDateTime.currentDateTime() currTime = data.toString("yyyy-MM-dd hh:mm:ss") self.update_date.emit( str(currTime) ) time.sleep(1) class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralWidget = QtWidgets.QWidget(MainWindow) self.centralWidget.setObjectName("centralWidget") self.gridLayout = QtWidgets.QGridLayout(self.centralWidget) self.gridLayout.setObjectName("gridLayout") self.pushButton = QtWidgets.QPushButton(self.centralWidget) self.pushButton.setObjectName("pushButton") self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1) self.textEdit = QtWidgets.QTextEdit(self.centralWidget) self.textEdit.setObjectName("textEdit") self.gridLayout.addWidget(self.textEdit, 1, 0, 1, 1) MainWindow.setCentralWidget(self.centralWidget) #下面将输出重定向到textEdit中 sys.stdout = EmittingStream(textWritten=self.handleDisplay) sys.stderr = EmittingStream(textWritten=self.handleDisplay) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "实时显示")) self.pushButton.setText(_translate("MainWindow", "开始")) def initUI(self): # 创建线程 self.backend = BackendThread() # 连接信号 self.backend.update_date.connect(self.handleDisplay) # 开始线程 self.backend.start() # 将当前时间输出到文本框 def handleDisplay(self, data): cursor = self.textEdit.textCursor() cursor.movePosition(QtGui.QTextCursor.End) # self.textEdit.setText(data) cursor.insertText(data + '\n') self.textEdit.setTextCursor(cursor) self.textEdit.ensureCursorVisible() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() win = Ui_MainWindow() win.setupUi(MainWindow) win.initUI() MainWindow.show() sys.exit(app.exec_())
- result
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。