Python Forum
[PyQt] Generate Progress Bar while executing a task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Generate Progress Bar while executing a task
#5
(Jul-12-2022, 02:27 PM)Axel_Erfurt Wrote: It might be better to show the progress bar when needed, e.g. in the status bar. Here's an example

    def download(self):
        self.completed = 0
        self.progress_bar.setFixedSize(self.geometry().width() - 120, 16)
        self.progress_bar.show()
        self.statusBar().showMessage("downloading ...", 0)

        while self.completed < 100:
            self.completed += 0.00005
            self.progress_bar.setValue(int(self.completed))
            
            if self.progress_bar.value() == 100:
                self.statusBar().showMessage("completed", 0)
                self.progress_bar.hide()
         # nothing will happen until we reach here

This example won't work unfortunately. In PyQt updates (adding widgets, changing things) only happen when you return to the event loop (by returning from your Python code). If you run the example, you'll notice that after pressing the button nothing happens, then *everything* happens at once: once the loop finishes, Qt gets to processing all the events you've created in one burst.

You can fudge this by calling QApplication.processEvents() in your loop -- although this wouldn't help for the real case with downloading a file as that's a single continuous process. The correct way to do this would be [using a separate thread, probably using QThreadPool](https://www.pythonguis.com/tutorials/mul...hreadpool/) that leaves the UI free to update. You can emit the progress from the worker using signals and then send that to any progressbar you want (popup or in the statusbar).
Reply


Messages In This Thread
RE: Generate Progress Bar while executing a task - by mfitzp - Aug-08-2022, 05:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How To Make A PyQt5 Progress Bar Run While Executing A Function rcwildabeast 1 536 Apr-26-2024, 02:18 AM
Last Post: menator01
  How can I measure progress and display it in the progress bar in the interface? Matgaret 2 6,070 Dec-11-2019, 03:30 PM
Last Post: Denni
  [Tkinter] Progress Bar While Sending an Email maxtimbo 3 4,290 Oct-09-2019, 09:13 PM
Last Post: woooee
  Progress Bar While Sending an Email maxtimbo 0 2,202 Oct-08-2019, 02:13 PM
Last Post: maxtimbo
  GUI Progress Bar Anysja 6 6,761 Aug-29-2018, 02:34 PM
Last Post: swetanjali
  [PyQt] cant import progress bar from another py file swipis 7 8,827 Dec-18-2016, 10:41 AM
Last Post: swipis

Forum Jump:

User Panel Messages

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