gpt4 book ai didi

python - PyQT:在窗口中获取实时打印输出

转载 作者:行者123 更新时间:2023-12-04 15:36:11 50 4
gpt4 key购买 nike

我正在开发一个 GUI,用于在按下按钮“my_button”时调用函数“my_function”。

该函数以迭代方式处理数据。它包含一个“for”循环,在每次迭代时,它都会打印出一条消息,显示我的函数的进度。我希望此打印件显示在我的 GUI 中(在此示例中为 textEdit 小部件),实时 .我怎么能那样做?

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

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

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 生成的类,而是创建另一个类,该类继承自适当的小部件并填充初始类。

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_())

关于python - PyQT:在窗口中获取实时打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59684629/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com