0

Consider this minimal code snippet :

from PyQt5.QtWidgets import QApplication,QWidget,QLabel

app=QApplication([])
w=QWidget()
l=QLabel(w)
l.setText("Ready")
w.show()

def load():
    l.setText('Loading...')
    some_function()
    l.setText('Loaded')

load()

The desired output here is that the QLabel text should change to 'Loading...' first and then after some_function() finishes it's task, it should change to 'Loaded'

But that isn't how it's working. The 'Loading...' text never appears. It jumps straight from 'Ready' to 'Loaded' after the completion of some_function(). Why is that happening?

Neeraj Kumar
  • 515
  • 7
  • 18
  • 1) add `from PyQt5.QtCore import QTimer` 2) remove `sleep(3)`, 3) change `l.setText('Bye')` to `QTimer.singleShot(3 * 1000, lambda: l.setText('Bye'))` – eyllanesc Jul 25 '19 at 02:18
  • Actually, in my application there is some function instead of sleep(sec). I just wrote that here for an example, my bad. Have made the appropriate changes to the post. – Neeraj Kumar Jul 25 '19 at 09:53
  • then add `import threading`, change `some_function()` to `threading.Thread(target= some_function, daemon=True).start()` – eyllanesc Jul 25 '19 at 10:04
  • But that will just create a parallel task and the current process control will shift to setText('Loaded') immediately. I don't think it would work. EDIT : Not working, as anticipated. – Neeraj Kumar Jul 25 '19 at 11:29
  • see https://gist.github.com/eyllanesc/14123c70b1dfc4941bdf1144cf6580cd – eyllanesc Jul 25 '19 at 11:40
  • I've never dealt with pyqtSignal(), emit() and pyqtSlot decorator. Would be very nice of you if you could comment those portions. Please ? _/'|'\_ – Neeraj Kumar Jul 25 '19 at 12:02
  • read the docs: https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html . These elements are the fundamental part of Qt. Read also https://doc.qt.io/qt-5/signalsandslots.html – eyllanesc Jul 25 '19 at 12:05

0 Answers0