Python Forum

Full Version: label doesn't update on each iteration
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
  I'm not new to programming, but I am new to Python.
  Can someone tell me why the label (lblTest) in my form (PyQt) doesn't update on each iteration? It only updates finally when the loop is complete.
  I've tried using ui.lblTest.update() and ui.lblTest.hide() and ui.lblTest.show() on each iteration (as I've seen suggested elsewhere), but it doesn't work.
def btnTest_clicked():
    x = 0
    while x < 4:
        print("Test: " + str(x))
        ui.lblTest.setText("Test: " + str(x))
        x = x + 1
        time.sleep(1.0)
Thanks,
David
It's possibly that the sleep is blocking the mailoop.
(Dec-11-2016, 01:40 AM)Yoriz Wrote: [ -> ]It's possibly that the sleep is blocking the mailoop.

Good thought. So I replaced the sleep with:

while y < 100000000:
   y = y + 1


But same result. The label won't update.
Hi all,
Found this: QtGui.QGuiApplication.processEvents() #update gui for pyqt
It works. But I don't know if this is just a dirty fix or the proper way of doing it.
But for now, I can move on.
David
A large loop will also block the mainloop. All the while your code is doing something the gui cannot update it's widgets.
Threading is used to move long running code out off the gui's mainloop.
Calls to the gui from the independent threads must be threadsafe.
Thank Yoriz. I get what you're saying. I'll keep that in mind when I do something larger.