PyQt5 – 进度条的背景图片
在这篇文章中,我们将看到如何为进度条设置一个背景图片。默认情况下,进度条没有设置背景图片,下面是普通进度条和有背景图片的进度条的样子。
为了做到这一点,我们必须使用CSS样式表,下面是样式表的代码。
QProgressBar
{
background-image : url(image.png);
border : 1px solid white;
}
这个样式表是通过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(30)
# setting alignment to center
bar.setAlignment(Qt.AlignCenter)
# setting background
bar.setStyleSheet(
"QProgressBar"
"{"
"background-image: url(image.png);"
" border: 1px solid white;"
"} "
)
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :