PyQt5 | How to change the process bar color?

PyQt5 | How to change the process bar color?

In PyQt5, you can change the progress bar color using stylesheets. Stylesheets in PyQt5 work similarly to CSS in web development, allowing you to change the appearance of widgets.

Below is an example of how to change the color of a QProgressBar widget:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar app = QApplication(sys.argv) # Create the main window window = QWidget() window.setWindowTitle('Progress Bar Color Example') # Create a vertical layout layout = QVBoxLayout() # Create a progress bar progressBar = QProgressBar() progressBar.setValue(50) # Set the value to 50 for demonstration purposes # Change the progress bar color using stylesheets progressBar.setStyleSheet(""" QProgressBar { border: 2px solid grey; border-radius: 5px; text-align: center; } QProgressBar::chunk { background-color: #05B8CC; width: 20px; /* used to be chunk size */ } """) # Add progress bar to the layout layout.addWidget(progressBar) # Set the layout on the main window window.setLayout(layout) # Show the window window.show() # Run the application sys.exit(app.exec_()) 

In the stylesheet applied to the QProgressBar:

  • The QProgressBar section styles the whole progress bar (e.g., border, border-radius, text alignment).
  • The QProgressBar::chunk section styles the filled part of the progress bar, which you're interested in changing. The background-color property changes the color of the progress. You can replace #05B8CC with any color you want, using either RGB hex codes or Qt color names.

When setting stylesheets, make sure your syntax is correct, as invalid stylesheets may not be applied and can be difficult to debug because PyQt does not throw explicit errors for them.


More Tags

kendo-dropdown facet-wrap windows-vista aspen periodicity alpine-linux little-man-computer nginx-config truncate soapui

More Programming Guides

Other Guides

More Programming Examples