PyQT:在 window 中获取实时打印输出

PyQT: get real time print output in window

我正在开发用于在按下按钮 'my_button' 时调用函数 'my_function' 的 GUI。

此函数迭代处理数据。它包含一个 'for' 循环,并且在每次迭代时打印出一条消息,显示我的函数的进度。我希望此打印件显示在我的 GUI 中(在本例中为文本编辑小部件),实时。我该怎么做?

我想说清楚,我需要实时显示。我在网上找到了一些解决方案,但是所有的打印都是在函数执行完成后才出现的。我需要打印件实时显示,以便了解功能进度。预先感谢您的提示!

这是一个最小的可复制示例(我通过 qt designer 获得了模板):

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(163, 225)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.formLayout = QtWidgets.QFormLayout(self.centralwidget)
        self.formLayout.setObjectName("formLayout")
        self.my_button = QtWidgets.QPushButton(self.centralwidget)
        self.my_button.setObjectName("my_button")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.my_button)
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.textEdit)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 163, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.my_button.clicked.connect(self.my_function)

    def my_function(self):
        for idx in range(10):
            print('Executing iteration '+str(idx)+' ...')
            time.sleep(1) # replaces time-consuming task
        print('Finished!')

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.my_button.setText(_translate("MainWindow", "Execute"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Qt 的黄金法则:您不应在 GUI 的主线程中执行消耗大量时间的任务,因为它们会阻塞 Qt 的事件循环。

考虑到以上情况,必须在另一个线程中执行耗时任务,并将信息通过信号发送给GUI线程。

另一方面,您不应修改 Qt Designer 生成的 class,而是创建另一个 class,它继承自适当的小部件并填充初始 class .

import threading
import time

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(163, 225)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.formLayout = QtWidgets.QFormLayout(self.centralwidget)
        self.formLayout.setObjectName("formLayout")
        self.my_button = QtWidgets.QPushButton(self.centralwidget)
        self.my_button.setObjectName("my_button")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.my_button)
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.textEdit)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 163, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.my_button.setText(_translate("MainWindow", "Execute"))


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    valueChanged = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.valueChanged.connect(self.on_value_changed)
        self.my_button.clicked.connect(self.on_clicked)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        threading.Thread(target=self.my_function, daemon=True).start()

    @QtCore.pyqtSlot(int)
    def on_value_changed(self, value):
        self.textEdit.append("Value: {}".format(value))

    def my_function(self):
        for idx in range(10):
            self.valueChanged.emit(idx)
            print("Executing iteration " + str(idx) + " ...")
            time.sleep(1)  # replaces time-consuming task
        print("Finished!")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())