Python Forum
[PyQt] label doesn't update on each iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] label doesn't update on each iteration
#1
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
Reply
#2
It's possibly that the sleep is blocking the mailoop.
Reply
#3
(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.
Reply
#4
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
Reply
#5
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.
Reply
#6
Thank Yoriz. I get what you're saying. I'll keep that in mind when I do something larger.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,938 Dec-18-2024, 03:05 AM
Last Post: Edward_
  update text variable on label with keypress knoxvilles_joker 5 7,941 May-31-2024, 02:09 PM
Last Post: menator01
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 1,930 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 5,786 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [Tkinter] how to update label text from list Roshan 8 7,580 Apr-25-2020, 08:04 AM
Last Post: Roshan
  Update a label text from subprocess jim53 3 5,728 Aug-19-2019, 08:21 PM
Last Post: Denni
  Unable to update or refresh label text in tkinter jenkins43 3 8,850 Jul-24-2019, 02:09 PM
Last Post: Friend
  Display and update the label text which display the serial value jenkins43 5 11,276 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  [Tkinter] can't update label to include variable foxtreat 2 4,898 Dec-01-2018, 07:16 AM
Last Post: jfong
  [Tkinter] Label doesn't refresh jollydragon 7 8,455 Jul-13-2018, 05:55 AM
Last Post: jollydragon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020