PyQt5 – 进度条的半透明条
在这篇文章中,我们将看到如何使进度条变成半透明的,即介于不透明和透明之间。进度条有两个组成部分,一个是背景,当进度条不在100%的时候是可见的,另一个是显示进度的条,当我们把条变成半透明的时候,背景就会可见。
为了做到这一点,我们必须改变阿尔法水平,即条形的透明度水平,下面是正常的进度条和半透明的进度条,背景颜色被设置为红色,条形颜色被设置为绿色。
为了改变alpha级别,我们必须改变CSS样式表,下面是进度条的样式表代码。
QProgressBar::chunk
{
background : rgba(0, 255, 0, 100);
}
此表用于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(80)
# setting alignment to center
bar.setAlignment(Qt.AlignCenter)
# setting background to color
# and bar color with alpha factor
bar.setStyleSheet("QProgressBar"
"{"
"background-color : rgba(255, 0, 0, 255);"
"border : 1px"
"}"
"QProgressBar::chunk"
"{"
"background : rgba(0, 255, 0, 100);"
"}"
)
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :