(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).