PyQt5 - Gradient Color Progress Bar

PyQt5 - Gradient Color Progress Bar

Creating a gradient color progress bar in PyQt5 involves customizing the QProgressBar widget. You can achieve this by applying a style sheet with a gradient effect to the progress bar. Here's a step-by-step guide to create a PyQt5 application with a gradient color progress bar:

  1. Install PyQt5: If you haven't installed PyQt5, you can do so using pip:

    pip install pyqt5 
  2. Create the Main Application: Start by setting up the main application and creating a main window.

  3. Add a QProgressBar: Add a QProgressBar widget to the window.

  4. Customize with StyleSheet: Apply a custom style sheet to the progress bar to create the gradient effect.

  5. Update the Progress Bar: Optionally, you can add functionality to update the progress bar's value.

Here's a complete example code:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QProgressBar, QVBoxLayout, QWidget from PyQt5.QtCore import Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle("Gradient Progress Bar Example") self.setGeometry(100, 100, 300, 100) # Create a QVBoxLayout instance layout = QVBoxLayout() # Create a QProgressBar self.progressBar = QProgressBar() self.progressBar.setMaximum(100) self.progressBar.setValue(45) # Set the custom styleSheet self.progressBar.setStyleSheet(""" QProgressBar { border: 2px solid grey; border-radius: 5px; text-align: center; } QProgressBar::chunk { background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #00ff00, stop:1 #0000ff); border-radius: 5px; } """) layout.addWidget(self.progressBar) # Create a central widget centralWidget = QWidget() centralWidget.setLayout(layout) # Set the central widget self.setCentralWidget(centralWidget) if __name__ == '__main__': app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() sys.exit(app.exec_()) 

In this example, the progress bar is styled with a linear gradient that transitions from green (#00ff00) to blue (#0000ff). You can customize the gradient stops and colors according to your preference. The QProgressBar::chunk style applies to the filled part of the progress bar, which is where the gradient is shown.


More Tags

pkcs#8 seaborn ms-word interface launch4j nouislider kafka-consumer-api database-trigger frames cython

More Programming Guides

Other Guides

More Programming Examples