0

I have the following simple code, and for some reason addItem() does not add an item to the List Widget in the app's UI in the exec_all() function. add_item() works perfectly fine outside of that function. Ive added the smallest snippet of the relevant code below:

from PyQt5 import QtWidgets, uic
from time import sleep

app = QtWidgets.QApplication([])
dlg = uic.loadUi("Program.ui")

def exec_all():
    dlg.OutputConsole.addItem("Test")
    web_scrap()
    # etc...

# web_scrap(): A function in another file that 
# scraps data from website. It needs to wait for
# HTML elements to fully load into DOM at times 
# by using time.sleep()
def web_scrap(dlg):
    sleep(5)

dlg.RunAllButton.clicked.connect(exec_all)

dlg.show()
app.exec()

UPDATE: Ive discovered where the problem lies by updating code snippet above to reproduce the problem -- it seems like addItem() does not want to execute before sleep() is called later, which is very odd to me unless addItem() is an asynchronous function? Will i have to use another method other than sleep() in web_scrap() in order to get around this problem?

6
  • Sorry but that code is completely insufficient. Please provide an appropriate minimal reproducible example. Commented Feb 14, 2024 at 22:42
  • Ive updated the code snippet that reproduces the problem. By doing so, ive discovered that the problem lies when calling sleep(). That is, addItem() only updates the UI AFTER sleep(), and not before Commented Feb 14, 2024 at 23:15
  • 1
    This is expected: operations such as sleep() or while/for loops are blocking, meaning that they prevent the event loop to properly process events, including requests for updates (aka: redraw widgets when required). If you add a print(dlg.OutputConsole.count()) after addItem() you'll see that it will properly print the correct element count of the list widget. Note that using an arbitrary time to wait for another "process" to end is a terrible idea: on a fast computer, you'll be waiting too much, on a slow one it will resume while the process hasn't completed yet. Commented Feb 15, 2024 at 1:14
  • 1
    In any case, if a time consuming operation doesn't need any communication with the UI, the threading module will suffice, but for short operations you may even consider calling QApplication.processEvents(). If you instead do need to interact with the UI (for example, to notify a finished "job"), you must use Qt signals/slots, because UI elements are not thread-safe; this means using QThread (either by subclassing, or to manage a QObject "worker" subclass) or eventually a QRunnable with a QObject "proxy" to emit signals. Commented Feb 15, 2024 at 1:31
  • I strongly suggest you to take your time and do some proper research on all the above topics, because there are plenty of related resources even here on StackOverflow, as you can see from the list of posts (which is just a partial compendium) for which I marked your question as duplicate. Commented Feb 15, 2024 at 1:32

1 Answer 1

0

So it turns out it was indeed the sleep() pausing execution of the entire thread of my pyqt app. This also caused my pyqt app to hang upon button click. See below for solution.

General problem: Some arbitrary amount of code wont run before a time.sleep() invocation in your python program.

Solution: Create a new thread for that problematic snippet of code by using the threading python module. For example:

from PyQt5 import QtWidgets, uic
from time import sleep
from threading import Thread

app = QtWidgets.QApplication([])
dlg = uic.loadUi("Program.ui")

def problematic_code():
    # ...
    sleep(5)
    # ...

def new_thread():
    newThread = Thread(target=problematic_code)
    newThread.start()


dlg.ButtonInYourApp.clicked.connect(new_thread)

Creating a second thread will allow your code to continue running as expected as well as keep your pyqt app from hanging with the spinning wheel.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.