鱼C论坛

 找回密码
 立即注册
查看: 621|回复: 23

进度条调入问题?

[复制链接]
发表于 2024-2-22 07:17:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 清風 于 2024-2-22 07:52 编辑

如题,CustomProgressBar.py单独运行是可以正常滚动的,
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QLabel
from PyQt6.QtCore import QTimer, 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.counter = 0

    def scrollToCenter(self):
        max_value = self.maximum()
        if max_value > 0:
            self.setValue(max_value)

    def startAnimation(self):
        self.timer = QTimer(self)
        self.timer.timeout.connect(self._animate)
        self.timer.start(100)

    def _animate(self):
        value = self.value() + 2
        if value > self.maximum():
            value = value % 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.scrollToCenter()
        progressBar.startAnimation()

        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = HuanYingWindow()
    window.show()
    sys.exit(app.exec())     


但是经如下代码调用后:
# 设置滚动条初始值为0
        self.progressBar.setValue(0)
        # 调用滚动条的方法
        self.progressBar.scrollToCenter()
        self.progressBar.startAnimation()
        self.progressBar._animate()
        # 判断滚动条是否达到 100%
        print(progressBar.value())
        input('...')
        if self.progressBar.value() == 100:
            # 打开主窗口
            mainWindow = MainWindow()
            mainWindow.show()
            self.close()  # 关闭当前窗口
        else:
            event.ignore()  # 忽略关闭事件

就不会滚动了,一闪而过.....

请教各位大佬,这是什么原因?

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-2-22 07:52:26 | 显示全部楼层
清風 发表于 2024-02-22 07:52
算了,我都迷糊了,我也不想调用了,拜托直接帮我把进 ...


好的,那我帮您将进度条代码直接写入程序中,请您尝试以下代码:import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QLabel
from PyQt6.QtCore import QTimer

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.counter = 0
   
    def scrollToCenter(self):
        max_value = self.maximum()
        if max_value > 0:
            self.setValue(max_value)
   
    def startAnimation(self):
        self.timer = QTimer(self)
        self.timer.timeout.connect(self._animate)
        self.timer.start(100)
   
    def _animate(self):
        value = self.value() + 2
        if value > self.maximum():
            value = value % 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.scrollToCenter()
        progressBar.startAnimation()
        self.setLayout(layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = HuanYingWindow()
    window.show()
    sys.exit(app.exec())请将此代码粘贴到您的程序中,并运行查看结果。如果有任何问题,请随时告诉我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:52:14 | 显示全部楼层
FishC 发表于 2024-2-22 07:49
非常抱歉再次给您带来了困扰。确保您已经安装了正确的PyQt版本,并尝试以下修改后的代码:from PyQt6.Q ...

算了,我都迷糊了,我也不想调用了,拜托直接帮我把进度条代码写进去程序中吧。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:49:26 | 显示全部楼层
清風 发表于 2024-02-22 07:49
还是报错

from PyQt6.QtGui import QPropertyAnima ...


非常抱歉再次给您带来了困扰。确保您已经安装了正确的PyQt版本,并尝试以下修改后的代码:from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QLabelfrom PyQt6.QtCore import QTimer, QPropertyAnimationclass 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.counter = 0    def scrollToCenter(self):        max_value = self.maximum()        if max_value > 0:            self.setValue(max_value)    def startAnimation(self):        animation = QPropertyAnimation(self, b"value")        animation.setDuration(3000)        animation.setStartValue(0)        animation.setEndValue(self.maximum())        animation.start()        self.setGraphicsEffect(animation)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([])    window = HuanYingWindow()    window.show()    app.exec()请再次尝试运行代码,看能否正常工作。如果问题仍然存在,请检查您的PyQt安装是否正确。如果仍然有问题,请告诉我您使用的是哪个版本的PyQt,并提供完整的错误信息,以便我能够更好地帮助您。谢谢!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:49:14 | 显示全部楼层
FishC 发表于 2024-2-22 07:46
非常抱歉,请接受我由衷的歉意。在上述代码中,`QAbstractAnimation`应该替换为`QAnimationGroup`以解 ...

还是报错

from PyQt6.QtGui import QPropertyAnimation
ImportError: cannot import name 'QPropertyAnimation' from 'PyQt6.QtGui' (D:\Python\Lib\site-packages\PyQt6\QtGui.pyd)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:46:27 | 显示全部楼层
清風 发表于 2024-02-22 07:46
这次直接报错:

animation.start(QAbstractAnimat ...


非常抱歉,请接受我由衷的歉意。在上述代码中,`QAbstractAnimation`应该替换为`QAnimationGroup`以解决报错问题,并使用以下方式导入所需的类:from PyQt6.QtCore import QPropertyAnimation, QSequentialAnimationGroup请根据下面的修改后代码示例重新运行程序:from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QLabel
from PyQt6.QtCore import QTimer
from PyQt6.QtGui import QPropertyAnimation

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.counter = 0

    def scrollToCenter(self):
        max_value = self.maximum()
        if max_value > 0:
            self.setValue(max_value)

    def startAnimation(self):
        animation = QPropertyAnimation(self, b"value")
        animation.setDuration(3000)
        animation.setStartValue(0)
        animation.setEndValue(self.maximum())
        animation.start(QPropertyAnimation.DeletionPolicy.DestroyWhenStopped)
        self.setGraphicsEffect(animation)

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([])
    window = HuanYingWindow()
    window.show()
    app.exec()请您再次尝试运行代码,看能否正常滚动。如果还有其他问题,请随时提问。谢谢!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:46:16 | 显示全部楼层
FishC 发表于 2024-2-22 07:44
非常抱歉再次给您带来困惑。我重新检查了代码,发现了问题所在。

在`CustomProgressBar`类中的`star ...

这次直接报错:

animation.start(QAbstractAnimation.DeletionPolicy.DestroyWhenStopped)
                    ^^^^^^^^^^^^^^^^^^
NameError: name 'QAbstractAnimation' is not defined
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:44:57 | 显示全部楼层
清風 发表于 2024-02-22 07:44
哈哈,这次QPropertyAnimation.py单独运行都没有滚动 ...


非常抱歉再次给您带来困惑。我重新检查了代码,发现了问题所在。

在`CustomProgressBar`类中的`startAnimation`方法中,我们需要将动画对象(`QPropertyAnimation`)添加到进度条对象上,以便启动动画效果。修改后的代码示例如下:

  1. def startAnimation(self):
  2.     animation = QPropertyAnimation(self, b"value")
  3.     animation.setDuration(3000)
  4.     animation.setStartValue(0)
  5.     animation.setEndValue(self.maximum())
  6.     animation.start(QAbstractAnimation.DeletionPolicy.DestroyWhenStopped)
  7.     self.setGraphicsEffect(animation)
复制代码


在上述代码中,我们使用 `self.setGraphicsEffect(animation)` 将动画对象设置为进度条的图形效果,`QPropertyAnimation` 会根据指定的起始值和结束值,在指定的时间内渐变地改变进度条的值。

请尝试运行这个修改后的代码,看是否能够正确滚动。如果仍然有问题,请提供更多详细信息,让我更好地理解您遇到的困难。非常抱歉之前的错误回答给您带来的困惑和不便。感谢您的耐心。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:44:49 | 显示全部楼层
FishC 发表于 2024-2-22 07:43
非常抱歉,之前的回答中确实有问题。下面是修改后的代码示例:

哈哈,这次QPropertyAnimation.py单独运行都没有滚动动作了!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:43:19 | 显示全部楼层
清風 发表于 2024-02-22 07:43
不行

QPropertyAnimation.py单独运行有滚动动作,
...


非常抱歉,之前的回答中确实有问题。下面是修改后的代码示例:

  1. import sys
  2. from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar
  3. from PyQt6.QtCore import QTimer, QPropertyAnimation

  4. class CustomProgressBar(QProgressBar):
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         self.setStyleSheet(
  8.             """
  9.             QProgressBar {
  10.                 text-align: center;
  11.                 color: white;
  12.                 border: none;
  13.                 background-color: #909090;
  14.                 height: 20px;
  15.             }
  16.             QProgressBar::chunk {
  17.                 background-color: #00A3E0;
  18.             }
  19.             """
  20.         )
  21.         self.setTextVisible(True)
  22.         self.counter = 0

  23.     def scrollToCenter(self):
  24.         max_value = self.maximum()
  25.         if max_value > 0:
  26.             self.setValue(max_value)

  27.     def startAnimation(self):
  28.         animation = QPropertyAnimation(self, b"value")
  29.         animation.setDuration(3000)
  30.         animation.setStartValue(0)
  31.         animation.setEndValue(self.maximum())
  32.         animation.start()

  33. class HuanYingWindow(QWidget):
  34.     def __init__(self):
  35.         super().__init__()
  36.         self.initUI()

  37.     def initUI(self):
  38.         layout = QVBoxLayout(self)
  39.         progressBar = CustomProgressBar()
  40.         layout.addWidget(progressBar)
  41.         progressBar.scrollToCenter()
  42.         progressBar.startAnimation()
  43.         self.setLayout(layout)

  44. if __name__ == "__main__":
  45.     app = QApplication(sys.argv)
  46.     window = HuanYingWindow()
  47.     window.show()
  48.     sys.exit(app.exec())
复制代码


这里使用了`QPropertyAnimation`来控制进度条的动画效果。在`startAnimation`方法中创建一个`QPropertyAnimation`对象,指定了起始值和结束值,并设置了动画的持续时间。然后调用`start`方法开始动画效果。

请尝试运行这个修改后的代码,看是否能正常滚动。如果仍然有问题,请提供更多详细信息,让我们更好地理解您遇到的困难。为之前的回答失误给您带来的不便再次道歉。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:43:07 | 显示全部楼层
FishC 发表于 2024-2-22 07:40
非常抱歉,以下是修改后的代码示例(注意Python格式):

不行

QPropertyAnimation.py单独运行有滚动动作,

但调用后仍然没有滚动动作。(好像初始值就是100)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:40:39 | 显示全部楼层
清風 发表于 2024-02-22 07:40
不行,

QPropertyAnimation.py单独运行有滚动动作 ...


非常抱歉,以下是修改后的代码示例(注意Python格式):

  1. import sys
  2. from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar
  3. from PyQt6.QtCore import QTimer

  4. class CustomProgressBar(QProgressBar):
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         self.setStyleSheet(
  8.             """
  9.             QProgressBar {
  10.                 text-align: center;
  11.                 color: white;
  12.                 border: none;
  13.                 background-color: #909090;
  14.                 height: 20px;
  15.             }
  16.             QProgressBar::chunk {
  17.                 background-color: #00A3E0;
  18.             }
  19.             """
  20.         )
  21.         self.setTextVisible(True)
  22.         self.counter = 0

  23.     def scrollToCenter(self):
  24.         max_value = self.maximum()
  25.         if max_value > 0:
  26.             self.setValue(max_value)

  27.     def startAnimation(self):
  28.         self.timer = QTimer(self)
  29.         self.timer.timeout.connect(self._animate)
  30.         self.timer.start(100)

  31.     def _animate(self):
  32.         value = self.value() + 2
  33.         if value > self.maximum():
  34.             value = value % self.maximum()
  35.         self.setValue(value)


  36. class HuanYingWindow(QWidget):
  37.     def __init__(self):
  38.         super().__init__()
  39.         self.initUI()

  40.     def initUI(self):
  41.         layout = QVBoxLayout(self)
  42.         progressBar = CustomProgressBar()
  43.         layout.addWidget(progressBar)
  44.         progressBar.scrollToCenter()
  45.         progressBar.startAnimation()
  46.         self.setLayout(layout)


  47. if __name__ == "__main__":
  48.     app = QApplication(sys.argv)
  49.     window = HuanYingWindow()
  50.     window.show()
  51.     sys.exit(app.exec())
复制代码


请尝试运行修正后的代码,看是否能够正常滚动。如果仍然有问题,请提供更多详细的信息,让我们更好地理解您遇到的困难。为之前的回答失误给您带来的不便再次道歉。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:40:25 | 显示全部楼层
FishC 发表于 2024-2-22 07:38
非常抱歉,以下是修改后的代码示例(注意Python格式):

不行,

QPropertyAnimation.py单独运行有滚动动作,

但调用后仍然没有滚动动作。


回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:38:32 | 显示全部楼层
清風 发表于 2024-02-22 07:38
回复请注意python格式


非常抱歉,以下是修改后的代码示例(注意Python格式):

  1. import sys
  2. from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar
  3. from PyQt6.QtCore import QPropertyAnimation, Qt

  4. class CustomProgressBar(QProgressBar):
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         self.setStyleSheet(
  8.             """
  9.             QProgressBar {
  10.                 text-align: center;
  11.                 color: white;
  12.                 border: none;
  13.                 background-color: #909090;
  14.                 height: 20px;
  15.             }
  16.             QProgressBar::chunk {
  17.                 background-color: #00A3E0;
  18.             }
  19.             """
  20.         )
  21.         self.setTextVisible(True)
  22.         self.animation = None  # 添加animation成员变量

  23.     def scrollToCenter(self):
  24.         self.setValue(0)

  25.     def startAnimation(self):
  26.         self.animation = QPropertyAnimation(self, b"value")  # 创建QPropertyAnimation对象
  27.         self.animation.setStartValue(0)
  28.         self.animation.setEndValue(100)
  29.         self.animation.setDuration(2000)
  30.         self.animation.setLoopCount(-1)
  31.         self.animation.start()

  32. class HuanYingWindow(QWidget):
  33.     def __init__(self):
  34.         super().__init__()
  35.         self.initUI()

  36.     def initUI(self):
  37.         layout = QVBoxLayout(self)
  38.         progressBar = CustomProgressBar()
  39.         layout.addWidget(progressBar)
  40.         progressBar.scrollToCenter()
  41.         progressBar.startAnimation()
  42.         self.setLayout(layout)

  43. if __name__ == "__main__":
  44.     app = QApplication(sys.argv)
  45.     window = HuanYingWindow()
  46.     window.show()
  47.     sys.exit(app.exec())
复制代码


请尝试运行修正后的代码,看是否能够正常滚动。再次为之前的回答失误给您带来的不便道歉。如果仍然有问题,请提供更多详细的信息,让我们更好地理解您遇到的困难。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:38:13 | 显示全部楼层
FishC 发表于 2024-2-22 07:37
非常抱歉给您带来的困扰。经过进一步的调查,发现问题出在对`QPropertyAnimation`对象的生命周期管理上 ...

回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:37:36 | 显示全部楼层
清風 发表于 2024-02-22 07:37
QPropertyAnimation.py单独运行有滚动动作了,

但 ...


非常抱歉给您带来的困扰。经过进一步的调查,发现问题出在对`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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:37:17 | 显示全部楼层
FishC 发表于 2024-2-22 07:35
非常抱歉给您带来困惑。实际上,在PyQt中使用`QPropertyAnimation`对`QProgressBar`进行动画处理是行不 ...

QPropertyAnimation.py单独运行有滚动动作了,

但是调用后又没动作了。。。。


我们好像又回到了问题的起点...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:35:17 | 显示全部楼层
清風 发表于 2024-02-22 07:35
不行,首先QPropertyAnimation.py单独运行就没有滚动 ...


非常抱歉给您带来困惑。实际上,在PyQt中使用`QPropertyAnimation`对`QProgressBar`进行动画处理是行不通的。在此向您道歉。

要实现进度条的滚动效果,可以使用PyQt提供的`QTimer`定时器结合`setValue()`方法来更新进度条的值。以下是修改后的代码示例:

  1. import sys
  2. from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar
  3. from PyQt6.QtCore import QTimer

  4. class CustomProgressBar(QProgressBar):
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         self.setStyleSheet(
  8.             """
  9.             QProgressBar {
  10.                 text-align: center;
  11.                 color: white;
  12.                 border: none;
  13.                 background-color: #909090;
  14.                 height: 20px;
  15.             }
  16.             QProgressBar::chunk {
  17.                 background-color: #00A3E0;
  18.             }
  19.             """
  20.         )
  21.         self.setTextVisible(True)
  22.    
  23.     def scrollToCenter(self):
  24.         self.setValue(0)
  25.    
  26.     def startAnimation(self):
  27.         self.timer = QTimer(self)
  28.         self.timer.timeout.connect(self._animate)
  29.         self.timer.start(100)
  30.    
  31.     def _animate(self):
  32.         value = self.value() + 1
  33.         if value > self.maximum():
  34.             value = value % (self.maximum() + 1)
  35.         self.setValue(value)

  36. class HuanYingWindow(QWidget):
  37.     def __init__(self):
  38.         super().__init__()
  39.         self.initUI()
  40.    
  41.     def initUI(self):
  42.         layout = QVBoxLayout(self)
  43.         progressBar = CustomProgressBar()
  44.         layout.addWidget(progressBar)
  45.         progressBar.scrollToCenter()
  46.         progressBar.startAnimation()
  47.         self.setLayout(layout)

  48. if __name__ == "__main__":
  49.     app = QApplication(sys.argv)
  50.     window = HuanYingWindow()
  51.     window.show()
  52.     sys.exit(app.exec())
复制代码


在修正后的代码中,我们使用`QTimer`定时器每隔一段时间触发一次`_animate`方法,通过递增进度条的值来实现滚动效果。请尝试运行修正后的代码,看是否能够正常滚动。如果仍然有问题,请提供更多详细的信息,让我们更好地理解您遇到的困难。再次为之前的回答失误给您带来的不便道歉。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 07:35:02 | 显示全部楼层
FishC 发表于 2024-2-22 07:31
抱歉,我之前的回答有误。在PyQt中,我们可以使用`QPropertyAnimation`来创建动画效果,从而实现进度条 ...

不行,首先QPropertyAnimation.py单独运行就没有滚动动作,停留在0%。。


回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 07:31:06 | 显示全部楼层
清風 发表于 2024-02-22 07:30
不能   还是没有滚动动作


抱歉,我之前的回答有误。在PyQt中,我们可以使用`QPropertyAnimation`来创建动画效果,从而实现进度条的滚动。以下是修正后的代码示例:

  1. import sys
  2. from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QProgressBar, QLabel
  3. from PyQt6.QtCore import QTimer, Qt, QPropertyAnimation

  4. class CustomProgressBar(QProgressBar):
  5.     def __init__(self, parent=None):
  6.         super().__init__(parent)
  7.         self.setStyleSheet(
  8.             """
  9.             QProgressBar {
  10.                 text-align: center;
  11.                 color: white;
  12.                 border: none;
  13.                 background-color: #909090;
  14.                 height: 20px;
  15.             }
  16.             QProgressBar::chunk {
  17.                 background-color: #00A3E0;
  18.             }
  19.             """
  20.         )
  21.         self.setTextVisible(True)
  22.         self.counter = 0

  23.     def scrollToCenter(self):
  24.         max_value = self.maximum()
  25.         if max_value > 0:
  26.             self.setValue(max_value)

  27.     def startAnimation(self):
  28.         animation = QPropertyAnimation(self, b"value")
  29.         animation.setDuration(1000)
  30.         animation.setStartValue(0)
  31.         animation.setEndValue(self.maximum())
  32.         animation.setLoopCount(-1)  # -1表示无限循环
  33.         animation.start()

  34. class HuanYingWindow(QWidget):
  35.     def __init__(self):
  36.         super().__init__()
  37.         self.initUI()

  38.     def initUI(self):
  39.         layout = QVBoxLayout(self)
  40.         progressBar = CustomProgressBar()
  41.         layout.addWidget(progressBar)
  42.         progressBar.scrollToCenter()
  43.         progressBar.startAnimation()
  44.         self.setLayout(layout)

  45. if __name__ == "__main__":
  46.     app = QApplication(sys.argv)
  47.     window = HuanYingWindow()
  48.     window.show()
  49.     sys.exit(app.exec())
复制代码


在修正后的代码中,我们使用`QPropertyAnimation`来创建一个动画对象,并将其应用于进度条的`value`属性。通过设置动画的起始值和结束值,以及循环次数,我们可以实现进度条滚动的效果。

请尝试运行修正后的代码,看是否能够正常滚动。如果仍然有问题,请提供更多详细的信息,让我们更好地理解您遇到的困难。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-26 05:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表