python_qt5控制臺(tái)內(nèi)容打印到textedit控件
代碼部分:
from PyQt5 import QtCore, QtGui
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import tqdm
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
? ?def setupUi(self, MainWindow):
? ? ? ?MainWindow.setObjectName("MainWindow")
? ? ? ?MainWindow.resize(1337, 634)
? ? ? ?self.centralwidget = QtWidgets.QWidget(MainWindow)
? ? ? ?self.centralwidget.setObjectName("centralwidget")
? ? ? ?self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
? ? ? ?self.groupBox.setGeometry(QtCore.QRect(100, 100, 741, 151))
? ? ? ?self.groupBox.setObjectName("groupBox")
? ? ? ?self.controlBrowser = QtWidgets.QTextBrowser(self.groupBox)
? ? ? ?self.controlBrowser.setGeometry(QtCore.QRect(10, 20, 721, 121))
? ? ? ?self.controlBrowser.setObjectName("controlBrowser")
? ? ? ?self.groupBox_wordvideo = QtWidgets.QGroupBox(self.centralwidget)
? ? ? ?self.groupBox_wordvideo.setGeometry(QtCore.QRect(100, 10, 501, 71))
? ? ? ?self.groupBox_wordvideo.setTitle("")
? ? ? ?self.groupBox_wordvideo.setObjectName("groupBox_wordvideo")
? ? ? ?self.butselectfiles = QtWidgets.QPushButton(self.groupBox_wordvideo)
? ? ? ?self.butselectfiles.setGeometry(QtCore.QRect(20, 30, 81, 23))
? ? ? ?self.butselectfiles.setObjectName("butselectfiles")
? ? ? ?MainWindow.setCentralWidget(self.centralwidget)
? ? ? ?self.menubar = QtWidgets.QMenuBar(MainWindow)
? ? ? ?self.menubar.setGeometry(QtCore.QRect(0, 0, 1337, 22))
? ? ? ?self.menubar.setObjectName("menubar")
? ? ? ?MainWindow.setMenuBar(self.menubar)
? ? ? ?self.statusbar = QtWidgets.QStatusBar(MainWindow)
? ? ? ?self.statusbar.setObjectName("statusbar")
? ? ? ?MainWindow.setStatusBar(self.statusbar)
? ? ? ?self.butselectfiles.clicked.connect(MainWindow.bClicked)
? ? ? ?self.retranslateUi(MainWindow)
? ? ? ?QtCore.QMetaObject.connectSlotsByName(MainWindow)
? ?def retranslateUi(self, MainWindow):
? ? ? ?_translate = QtCore.QCoreApplication.translate
? ? ? ?MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
? ? ? ?self.groupBox.setTitle(_translate("MainWindow", "信息欄"))
? ? ? ?self.butselectfiles.setText(_translate("MainWindow", "打印輸出"))
class EmittingStr(QtCore.QObject):
? ?textWritten = QtCore.pyqtSignal(str) ?# 定義一個(gè)發(fā)送str的信號(hào)
? ?def write(self, text):
? ? ? ?self.textWritten.emit(str(text))
class ControlBoard(QMainWindow, Ui_MainWindow):
? ?def __init__(self):
? ? ? ?super(ControlBoard, self).__init__()
? ? ? ?self.setupUi(self)
? ? ? ?# 下面將輸出重定向到controlBrowser中
? ? ? ?sys.stdout = EmittingStr(textWritten=self.outputWritten)
? ? ? ?sys.stderr = EmittingStr(textWritten=self.outputWritten)
? ? ? ?# self.pushButton.clicked.connect(self.bClicked)
? ?'''控制臺(tái)內(nèi)容打印到textEdit控件'''
? ?def outputWritten(self, text):
? ? ? ?cursor = self.controlBrowser.textCursor() ?#獲得指針對(duì)象
? ? ? ?cursor.movePosition(QtGui.QTextCursor.End)
? ? ? ?cursor.insertText(text)
? ? ? ?self.controlBrowser.setTextCursor(cursor)
? ? ? ?self.controlBrowser.ensureCursorVisible()
? ?def bClicked(self):
? ? ? ?"""Runs the main function."""
? ? ? ?print('Begin')
? ? ? ?lt = ["a", "b", "c", "d"]
? ? ? ?for i in tqdm.tqdm(lt):
? ? ? ? ? ?# i = "a", "b", "c", "d"
? ? ? ? ? ?time.sleep(1)
? ? ? ?self.printABCD()
? ? ? ?print("End")
? ?def printABCD(self):
? ? ? ?print("aaaaaaaaaaaaaaaa")
? ? ? ?print("bbbbbbbbbbbbbbbb")
? ? ? ?print("cccccccccccccccc")
? ? ? ?print("dddddddddddddddd")
if __name__ == "__main__":
? ?app = QApplication(sys.argv)
? ?win = ControlBoard()
? ?win.show()
? ?# win.printABCD()
? ?sys.exit(app.exec_())