PyQt QProgressBar

The figure above shows the basic progress bar for macOS and Windows 7, respectively. The progress bar is a widget used for time-consuming tasks. (See QProgressBar official document)

The QProgressBar widget provides a horizontal, vertical progress bar. You can set the minimum and maximum values for the progress bar with the setMinimum() and setMaximum() methods, or you can set a range at a time with the setRange() method. The default values are 0 and 99.

Related course: Create Desktop Apps with Python PyQt5

QProgressBar.

The setValue() method allows you to set the progress of the progress bar to a specific value, and the reset() method returns to its initial state.

If you set both the minimum and maximum values in the progress bar to 0, the progress bar is always in progress, as shown in the figure above. This can be useful when you don't know how many files you're downloading.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar
from PyQt5.QtCore import QBasicTimer

class MyApp(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        self.timer = QBasicTimer()
        self.step = 0

        self.setWindowTitle('QProgressBar')
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def timerEvent(self, e):
        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            return

        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self):
        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self)
            self.btn.setText('Stop')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Created horizontal progress bars and push buttons one by one.

The push button allows you to start and stop the progress bar.

pyqt qprogressbar

Description

self.pbar = QProgressBar(self)

Create a progress bar with the QProgressBar constructor.

self.timer = QBasicTimer()

To activate the progress bar, use a timer object.

self.timer.start(100, self)

To execute the timer event, call the start() method.

This method has two parameters: the first is the end time and the second is the object on which the event will be performed.

def timerEvent(self, e):

    if self.step >= 100:
        self.timer.stop()
        self.btn.setText('Finished')
        return

    self.step = self.step + 1
    self.pbar.setValue(self.step)

Each QObject and its descendants have a timerEvent() event handler. Reconfigures event handlers to respond to timer events.

def doAction(self):

if self.timer.isActive():
    self.timer.stop()
    self.btn.setText('Start')
else:
    self.timer.start(100, self)
    self.btn.setText('Stop')

Within the doAction() method, it starts and stops the timer.

Related course: Create Desktop Apps with Python PyQt5