Equivalent to Time.Sleep For a Pyqt Application

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("...")

GUI freezes on sleep from a worker

After many many hours of searching online, I found that using QtTest.QTest.qWait(msecs)
instead of sleep is giving me the wanted result - it holds the commands but does not make the GUI freeze.
In order to use it I had to use from PyQt5 import QtTest.

If anyone has yet a better suggestions (to any of the questions above) I would love to see them.

PyQT5 show label before sleep

As you said self.varakozas is QLabel so i predict it is child of your window.

If you call show() Qt will only schedule paint event for this widget and paint event is called by event loop. Event loop is executed once the execution of this method is done. So the sleep is called first because it is before the end of scope. This is standard behaviour of Qt event loop environments.

But there is a workaround. You can call paint event manually by repaint() method.

Try this:

def status_check(self):
self.varakozas.setText("Please wait...")
self.varakozas.show()
self.repaint()
time.sleep(1.25)

time.sleep() crashes program when using while-loop in PyQt

You should call QtCore.QCoreApplication.processEvents() within your for loop to make the Qt's event loop proceed the incoming event (from keyboard or mouse or sleep).

Although calling QtCore.QCoreApplication.processEvents() works , I have read in many places on the web that it should be a last resort. Unfortunately none of the sources clearly explain why -- but see for example

  • How to make Qt work when main thread is busy?
  • Should I use QCoreApplication::processEvents() or QApplication::processEvents()?
  • How to implement a QThread that runs forever{} with a QWaitCondition but still needs to catch another Slot while doing that
  • http://qt-project.org/forums/viewthread/22967

So it does seem allowed, but in general, it seems to be better design to use QTimer or QThread.



Related Topics



Leave a reply



Submit