|
发表于 2024-2-22 07:37:36
|
显示全部楼层
非常抱歉给您带来的困扰。经过进一步的调查,发现问题出在对`QPropertyAnimation`对象的生命周期管理上。在代码中,我们没有将`QPropertyAnimation`对象作为成员变量保存在`CustomProgressBar`类中,导致其在函数执行完毕后被销毁,进而导致动画无法正常播放。以下是修改后的代码示例:import sysfrom PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBarfrom PyQt6.QtCore import QPropertyAnimation, Qt, QSequentialAnimationGroup, QTimerclass 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.animation = None # 添加animation成员变量 def scrollToCenter(self): self.setValue(0) def startAnimation(self): self.animation = QPropertyAnimation(self, b"value") # 创建QPropertyAnimation对象 self.animation.setStartValue(0) self.animation.setEndValue(100) self.animation.setDuration(2000) self.animation.setLoopCount(-1) self.animation.start()class HuanYingWindow(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)if __name__ == "__main__": app = QApplication(sys.argv) window = HuanYingWindow() window.show() sys.exit(app.exec())在修正后的代码中,我们将`QPropertyAnimation`对象作为成员变量保存在`CustomProgressBar`类中,保证其在函数执行完毕后不被销毁。同时,我们使用`setStartValue()`、`setEndValue()`和`setDuration()`方法来设置动画的起始值、结束值和持续时间。通过调用`start()`方法启动动画,并且将循环次数设置为-1,使得动画可以无限循环播放。请尝试运行修正后的代码,看是否能够正常滚动。再次为之前的回答失误给您带来的不便道歉。如果仍然有问题,请提供更多详细的信息,让我们更好地理解您遇到的困难。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|