📜  PyQt5 – 图像栏作为进度条

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

PyQt5 – 图像栏作为进度条

在本文中,我们将了解如何将图像添加到进度条。我们可以设置背景图像,但为了将图像设置为条形图,我们必须修改进度条块 CSS,下面是正常背景图像和条形图的背景图像的样子。

为了做到这一点,下面是块文件的 CSS 样式表

QProgressBar::chunk
{
 background-image : url(image.png);
}

此样式表由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, 100, 200, 30)
  
        # setting the value
        bar.setValue(70)
  
        # setting alignment to center
        bar.setAlignment(Qt.AlignCenter)
  
        # adding background image to bar
        bar.setStyleSheet(
                          "QProgressBar::chunk "
                          "{"
                          "background-image: url(image.png);"
                          "}"
                          )
  
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :