time.sleep() and BackGround Windows PyQt5

An expression equivalent to time.sleep(2) that is friendly to PyQt is as follows:

loop = QEventLoop()
QTimer.singleShot(2000, loop.quit)
loop.exec_()

The problem is caused because you are showing the widget after the pause, you must do it before calling run(), Also another error is to use QImage, for questions of widgets you must use QPixmap.

If the file does not exist then QPixmap will be null and to know if it is you should use the isNull() method:

    [...]

    self.setGeometry(0, 0, self.wLoadDisplay, self.hLoadDisplay)
    palette = self.palette()
    pixmap = QPixmap("bgloading.png")
    if not pixmap.isNull():
        pixmap = pixmap.scaled(QSize(self.wLoadDisplay, self.hLoadDisplay))
        palette.setBrush(QPalette.Window, QBrush(pixmap))
    else:
        palette.setBrush(QPalette.Window, QBrush(Qt.white))
    self.setPalette(palette)

    qtRectangle = self.frameGeometry()
    centerPoint = QDesktopWidget().availableGeometry().center()
    qtRectangle.moveCenter(centerPoint)
    self.move(qtRectangle.topLeft())
    self.show()

    self.run()

[...]

def reset(self):
    loop = QEventLoop()
    QTimer.singleShot(2000, loop.quit)
    loop.exec_()
    self.pbar.setMaximum(0)
    self.pbar.setValue(0)
    self.label.setText("...")

Leave a Comment