1

I'm currently having issues with the following code (python 3.4.3 using PyQt5) :

    while not joblist ==[]:          

        currentitem = joblist.pop()
        self.lstthreads.item(threadnum-1).setText("thread" + str(threadnum) + " - " + str(len(joblist)) + " items left to process.")

This snippet is inside a thread I first load a huge textfile into a list then split it into 5 parts. Each part runs in the thread above. As you can see a item will be removed each loop using pop() and a listbox (pyqt) is updated with the total amount of items left in the list after pop() for tracking progress.

However the listbox shows: Thread1 - 0 items left to process. Thread2 - 0 items left to process. Thread3 - 0 items left to process. Thread4 - 0 items left to process. Thread5 - 0 items left to process.

The weird thing however is that if I add a print statement like so:

    while not joblist ==[]:          
        print ("trash")
        currentitem = joblist.pop()
        self.lstthreads.item(threadnum-1).setText("thread" + str(threadnum) + " - " + str(len(joblist)) + " items left to process.")

I get what I expected: Thread1 - 1396 items left to process. Thread2 - 1396 items left to process. Thread3 - 1396 items left to process. Thread4 - 1396 items left to process. Thread5 - 1396 items left to process.

The Listbox items also update as expected , why is this?

  • 1
    Please follow [MCVE]{http://stackoverflow.com/help/mcve}. You're close, but we could really use a minimal data set to reproduce the problem for ourselves. Fixing code by eyeball is a chancy prospect. – Prune Oct 25 '15 at 02:17
  • 1
    Updating a listbox from a thread is not safe. You should only interact with GUI objects from the main thread. Thus, you cannot rely on the listbox displaying correct information in your example. You may still have another issue (which the `print` statement solves by slowing down the threads), but first fix the listbox updating by instead emitting a signal from your thread (needs to be a `QThread`) to your main thread, which then updates the listbox ([this](http://stackoverflow.com/a/21071865/1994235) answer has an example of a `QThread` emitting a signal). – three_pineapples Oct 25 '15 at 05:28

1 Answers1

-1

Your main thread is not running because of the other threads, don't know the reason without the full code, but calling QCoreApplication.processEvents () should solve this.

Dragos Pop
  • 428
  • 2
  • 8