非常抱歉,以下是修正后的代码示例:
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtWidgets import QProgressBar, QApplication, QWidget, QVBoxLayout
class CustomProgressBar(QProgressBar):
def __init__(self, parent=None):
super().__init__(parent)
self.timer = QTimer(self)
self.timer.timeout.connect(self._animate)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
bgRect = self.rect()
bgRect.adjust(1, 1, -1, -1)
painter.fillRect(bgRect, QColor(240, 240, 240))
progressRect = bgRect
progressRect.setWidth(progressRect.width() * self.value() / self.maximum())
painter.fillRect(progressRect, QColor(0, 120, 230))
pen = QPen(QColor(200, 200, 200))
pen.setWidth(2)
painter.setPen(pen)
painter.drawRect(bgRect)
def scrollToCenter(self):
max_value = self.maximum()
if max_value > 0:
self.setValue(max_value // 2)
def startAnimation(self):
self.timer.start(100)
def _animate(self):
value = self.value() + 5
if value > self.maximum():
value = value % self.maximum()
self.setValue(value)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout(self)
progressBar = CustomProgressBar()
layout.addWidget(progressBar)
progressBar.scrollToCenter()
progressBar.startAnimation()
self.setLayout(layout)
self.show()
if __name__ == "__main__":
app = QApplication([])
mainWindow = MainWindow()
app.exec_()
这次的代码修复了之前的格式问题,可以直接复制粘贴运行,希望能解决您的问题。如果还有其他疑问,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |