QProgressBar is a widget to show process. You’ve likely seen it many times during installations.
The widget shows a bar and you can see the percentage completed. You can set its value with
the method setValue(). Where 50 would set it to 50%.
Related Course: Create GUI Apps with Python PyQt5
QProgressBar
Progressbar
Use the code below to create a progressbar:1
2self.pbar = QProgressBar(self)
self.pbar.setGeometry(30, 40, 200, 25)
You can set the value with:
1 | self.pbar.setValue(50) |
That’s all that’s needed to create a progressbar.
To update it’s value, you can use a QTimer.
1 | from PyQt5.QtCore import QBasicTimer |
Call a method every second with these lines:1
2self.timer = QTimer() self.timer.timeout.connect(self.handleTimer)
self.timer.start(1000)
Then update the progressbar value:1
2
3
4
5
6
7
8def handleTimer(self):
value = self.pbar.value()
if value < 100:
value = value + 1
self.pbar.setValue(value)
else:
self.timer.stop()
Example
Copy the code below to see a progressbar counting from 50% to 100%.
The progressbar is updated using the method handleTimer() and a QTimer().
1 | import sys |
If you are new to Python PyQt, then I highly recommend this book.