📜  PyQt5 - 进度条的渐变色条

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

PyQt5 - 进度条的渐变色条

在本文中,我们将看到如何为进程栏的栏创建渐变颜色。

渐变颜色:在计算机图形学中,颜色渐变指定与位置相关的颜色范围,通常用于填充区域。例如,许多窗口管理器允许将屏幕背景指定为渐变。渐变产生的颜色随位置连续变化,产生平滑的颜色过渡。

下面是进度条的正常条与进度条的渐变颜色条的外观:

样式表代码:

QProgressBar::chunk 
{
background: QLinearGradient( x1: 0, y1: 0,
                             x2: 1, y2: 0, 
                            stop: 0 #000fff,
                            stop: 1 #ff000f );
}

此样式表在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 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, 150, 200, 30)
  
        # set value to progress bar
        bar.setValue(100)
  
        # setting gradient color to bar
        bar.setStyleSheet("QProgressBar::chunk "
                          "{"
                          "background: QLinearGradient( x1: 0, y1: 0, 
                                                        x2: 1, y2: 0, 
                                                    stop: 0 # 00ffff, 
                                                   stop: 1 # ff000f );"
                          "}")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :