In this article we will see how to create capsule shaped progress bar. By default when we create progress bar, it is in rectangular shape although we can change the shape with the help of resize
method. But this will only allows to change the shape from rectangle to square. Below is representation of normal progress bar vs the capsule shaped progress bar.
In order to do so we have to change the radius of the border of both background and the bar of progress bar, this is done by changing the CSS style sheet, below is the style sheet code.
QProgressBar { border: solid grey; border-radius: 15px; color: black; } QProgressBar::chunk { background-color: #05B8CC; border-radius :15px; }
This style sheet is used which setStyleSheet
method, below is the implementation.
# importing libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * from PyQt5.QtCore import * import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
# setting title
self .setWindowTitle( "Python " )
# setting background color to window
# self.setStyleSheet("background-color : yellow")
# setting geometry
self .setGeometry( 100 , 100 , 600 , 400 )
# calling method
self .UiComponents()
# showing all the widgets
self .show()
# method for widgets
def UiComponents( self ):
# creating progress bar
bar = QProgressBar( self )
# setting geometry to progress bar
bar.setGeometry( 200 , 100 , 200 , 30 )
# setting the value
bar.setValue( 70 )
# setting alignment to center
bar.setAlignment(Qt.AlignCenter)
# setting background to color and radius
# and bar color and bar radius
bar.setStyleSheet( "QProgressBar"
"{"
"border: solid grey;"
"border-radius: 15px;"
" color: black; "
"}"
"QProgressBar::chunk "
"{background-color: # 05B8CC;"
"border-radius :15px;"
"}" )
App = QApplication(sys.argv)
# create the instance of our Window window = Window()
# start the app sys.exit(App. exec ())
|
Output :