PyQt5 - Gradient color Bar of Progress Bar

PyQt5 - Gradient color Bar of Progress Bar

Creating a gradient color bar for a progress bar in PyQt5 involves customizing the QProgressBar widget using style sheets (CSS-like syntax used in Qt for styling). You can define a linear gradient for the progress bar's background to create a gradient effect.

Here's an example of how to create a PyQt5 application with a progress bar that has a gradient color bar:

1. Import Required PyQt5 Modules

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar from PyQt5.QtCore import Qt import sys 

2. Create and Style the Progress Bar

Define a function to create and style the progress bar:

def create_gradient_progress_bar(): progress_bar = QProgressBar() progress_bar.setAlignment(Qt.AlignCenter) progress_bar.setValue(50) # Set a test value # Style the progress bar with a gradient progress_bar.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 #FF0000, stop: 1 #0000FF); } """) return progress_bar 

In the style sheet:

  • QProgressBar styles the overall progress bar (border, border-radius, etc.).
  • QProgressBar::chunk styles the actual progress indicator. Here, a horizontal linear gradient is used from red (#FF0000) to blue (#0000FF). Adjust the stop values and colors to customize the gradient.

3. Set Up the Application and Layout

Create the main application, the main window, and add the progress bar:

app = QApplication(sys.argv) window = QWidget() layout = QVBoxLayout(window) gradient_progress_bar = create_gradient_progress_bar() layout.addWidget(gradient_progress_bar) window.setLayout(layout) window.show() sys.exit(app.exec_()) 

Complete Example

Combining all the parts, the complete Python script for the PyQt5 application with a gradient progress bar is as follows:

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar from PyQt5.QtCore import Qt import sys def create_gradient_progress_bar(): progress_bar = QProgressBar() progress_bar.setAlignment(Qt.AlignCenter) progress_bar.setValue(50) # Set a test value progress_bar.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 #FF0000, stop: 1 #0000FF); } """) return progress_bar app = QApplication(sys.argv) window = QWidget() layout = QVBoxLayout(window) gradient_progress_bar = create_gradient_progress_bar() layout.addWidget(gradient_progress_bar) window.setLayout(layout) window.show() sys.exit(app.exec_()) 

Run this script to display a window with a progress bar featuring a horizontal gradient. You can adjust the gradient colors and the value of the progress bar as needed for your application.


More Tags

lstm android-studio-import decompiling google-translate router jdbctemplate verification authorize-attribute git-difftool azure-servicebus-queues

More Programming Guides

Other Guides

More Programming Examples