PyQt5 – 将图像条作为进度条
在这篇文章中,我们将看到如何在进度条上添加图片。我们可以设置背景图片,但是为了给条形图设置图片,我们必须修改进度条的大块CSS,下面是普通背景图片和条形图背景的样子。
为了做到这一点,下面是chunk文件的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())
输出 :