📜  PyQt5 - 进度条的半透明条

📅  最后修改于: 2022-05-13 01:54:34.535000             🧑  作者: Mango

PyQt5 - 进度条的半透明条

在本文中,我们将看到如何使条形图半透明,即介于不透明和透明之间。进度条有两个组成部分,一个是在进度条不是 100% 时可见的背景,另一个是告诉进度的进度条,当我们将进度条设为半透明时,背景将可见。

为此,我们必须更改 alpha 级别,即条的透明度级别,下面是普通进度条与半透明进度条,背景颜色设置为红色,条颜色设置为绿色。

为了改变 alpha 级别,我们必须改变 CSS 样式表,下面是 bar 的样式表代码。

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())

输出 :