Creating a QMainWindow >> pushing start button >> that connects a long-running function with a QLabel as arg >> updating the label while running the long function.
I want to update the status of the long-running function in the GUI. But as soon as the long-running function starts the whole window freezes
import sys
import time
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
def runLongTask(label):
label.setText('<1> sleeping for 10s ...')
time.sleep(10)
label.setText('<2> sleeping for 10s ...')
time.sleep(10)
label.setText('End')
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def setupUi(self):
self.setWindowTitle("GUI freeze FIX")
self.resize(350, 250)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.label = QLabel()
self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.label.setText('No Update')
countBtn = QPushButton("Start")
countBtn.clicked.connect(lambda: runLongTask(self.label))
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(countBtn)
self.centralWidget.setLayout(layout)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())