|
发表于 2024-2-21 22:38:54
|
显示全部楼层
非常抱歉,给您带来了困惑。这里是因为在调用startAnimation方法时传递了一个参数,而在CustomProgressBar类中定义的startAnimation方法不接受任何参数。
要解决这个问题,可以通过将鼠标事件绑定到CustomProgressBar上来实现滚动条滚动到中间的效果。以下是修改后的代码示例:
- import sys
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar
- from PyQt5.QtGui import QPainter, QColor, QPen
- from PyQt5.QtCore import Qt
- class CustomProgressBar(QProgressBar):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setStyleSheet(
- """
- QProgressBar {
- text-align: center;
- color: white;
- border: none;
- background-color: #909090;
- height: 20px;
- }
- QProgressBar::chunk {
- background-color: #00A3E0;
- }
- """
- )
- self.setTextVisible(True)
- self.setMouseTracking(True)
-
- def mouseMoveEvent(self, event):
- if event.buttons() == Qt.LeftButton:
- value = int((event.x() / self.width()) * self.maximum())
- self.setValue(value)
-
- def mousePressEvent(self, event):
- if event.button() == Qt.LeftButton:
- value = int((event.x() / self.width()) * self.maximum())
- self.setValue(value)
- class HuanYingWindow(QWidget):
- def __init__(self):
- super().__init__()
- self.initUI()
-
- def initUI(self):
- layout = QVBoxLayout(self)
- progressBar = CustomProgressBar()
- layout.addWidget(progressBar)
- progressBar.setMinimum(0)
- progressBar.setMaximum(100)
- self.setLayout(layout)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- window = HuanYingWindow()
- window.show()
- sys.exit(app.exec_())
复制代码
运行以上代码,您将会看到一个带有自定义样式的进度条控件。当您在进度条上移动鼠标或点击时,进度条的值会相应地更新。这样就实现了滚动条滚动到百分比中间的效果。
如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|