Updating Python GUI element from Qthread

前端 未结 1 366
闹比i
闹比i 2021-01-16 22:50

So I know there are a lot of posts about updating elements in a GUI with Qthread. I did my best to go over these, but still have a question.

I\'m trying to create a

相关标签:
1条回答
  • 2021-01-16 23:04

    There are a few things wrong with your code, however you were pretty close.

    The first obvious one is that you have a loop with a time.sleep() in your main thread. The Ui_Dialog.get_time() method runs in the main thread (as it should). You should not have any long running code there. However, the loop with the time.sleep(2) in it is long running code. As it stands now, your GUI locks up because control it not returned to the GUI event loop for 2*countto seconds. Just remove the whole while loop. I don't really know why it is there.

    Delete this:

    q = 0
    while q < 5:
        print(countto+2)
        time.sleep(2)
        q += 1
    

    The next issue comes because you recreate the QThread each time you click the button. As such, for each new object, you need to connect the updateProgress signal in that object to the setProgress slot. So change the code there to:

    self.wt = WorkerThread(countto)        
    self.wt.updateProgress.connect(self.setProgress)
    self.wt.start()
    

    At this point you will see the progress bar update correctly. However, the maximum value of the progress bar is set to 100 by default. So you might want to set the maximum value to countto just before you create the thread. For example:

    self.progressBar.setMaximum(countto)
    self.wt = WorkerThread(countto)    
    

    Hope that explains things!

    0 讨论(0)
提交回复
热议问题