PyQt5 QProgressBar - How to create progress bar?

PyQt5 QProgressBar - How to create progress bar?

QProgressBar is a widget provided by PyQt5 to create a horizontal or vertical progress bar. Here's a step-by-step guide on how to create and use a QProgressBar with PyQt5:

1. Installing PyQt5:

If you haven't installed PyQt5 yet:

pip install pyqt5 

2. Basic Usage of QProgressBar:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QProgressBar, QVBoxLayout, QPushButton from PyQt5.QtCore import QTimer class ProgressBarApp(QWidget): def __init__(self): super().__init__() # Initialize the progress bar self.progress_bar = QProgressBar(self) # Set the maximum value of the progress bar (default is 100) self.progress_bar.setMaximum(100) # Create a QPushButton to start the progress self.button = QPushButton("Start Progress", self) self.button.clicked.connect(self.start_progress) # Set up a vertical layout layout = QVBoxLayout() layout.addWidget(self.progress_bar) layout.addWidget(self.button) # Set the layout for the widget self.setLayout(layout) # Set up a timer to simulate updating the progress bar self.timer = QTimer() self.timer.timeout.connect(self.handle_timeout) self.counter = 0 def start_progress(self): # Start or restart the progress bar self.counter = 0 self.progress_bar.setValue(self.counter) self.timer.start(100) # Update every 100 ms def handle_timeout(self): # Increment the counter and update the progress bar self.counter += 1 self.progress_bar.setValue(self.counter) # Stop the timer when the progress reaches its maximum value if self.counter >= self.progress_bar.maximum(): self.timer.stop() if __name__ == "__main__": app = QApplication(sys.argv) window = ProgressBarApp() window.show() sys.exit(app.exec_()) 

This example showcases:

  • A basic window with a progress bar and a button.
  • When the button is clicked, it simulates a progress over time using a QTimer.
  • The progress bar fills up over time until it reaches its maximum value.

You can adjust the QTimer interval and the step size to increase or decrease the speed at which the progress bar fills up.


More Tags

upsert pdflatex windows-7 geopandas prettier php-carbon kendo-datepicker web-config amazon-redshift rightbarbuttonitem

More Programming Guides

Other Guides

More Programming Examples