I'm working on a GUI for calling a function 'my_function' when the button 'my_button' is pushed.
This function processes data iteratively. It contains a 'for' loop, and at each iteration it prints out a message that shows the progress of my function. I would like this prints to be displayed in my GUI (in this example in a textEdit widget), in real time. How could I do that?
I would like to make it clear that I need real time display. I found some solutions online, but all prints only appear when the function finishes execution. I need the prints to display in real time in order to appreciate the function progress. Thanks in advance for your tips!
Here's a minimal reproductible example (I obtained the template via 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_())
time.sleep(1)for? Do you use it for a delay or is it to replace a time-consuming task?