PyQt5 – 胶囊状的进度条
在这篇文章中,我们将看到如何创建胶囊状的进度条。默认情况下,当我们创建进度条时,它是长方形的,尽管我们可以通过调整大小的方法来改变它的形状。但这只允许将形状从矩形改为方形。下面是普通进度条与胶囊形状的进度条的对比。
为了做到这一点,我们必须改变进度条的背景和条形的边界半径,这是通过改变CSS样式表来实现的,下面是样式表代码。
QProgressBar
{
border: solid grey;
border-radius: 15px;
color: black;
}
QProgressBar::chunk
{
background-color: #05B8CC;
border-radius :15px;
}
Python
这个样式表被用于setStyleSheet方法,下面是实现。
# 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())
Python
输出 :