鱼C论坛

 找回密码
 立即注册
查看: 1650|回复: 5

[已解决]进度条问题~

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

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

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

x
进度条终于调用成功了,并且也有滚动动作了。

但是: 问题1:每次滚动的起点数好像是从上次停止的数字开始滚动的,而我的要求是每次启动程序都从0开始滚动;

          问题2:每次滚动一直在循环进行,而我的要求是到100就关闭huanYing窗体,然后自动调用main窗体。。。
最佳答案
2024-2-22 15:07:02
问题1:每次滚动的起点数好像是从上次停止的数字开始滚动的,而我的要求是每次启动程序都从0开始滚动的。

可以使用一个变量来记录当前的进度条位置,每次启动程序时将其置为0即可。具体代码如下:

  1. import time

  2. def progress_bar():
  3.     width = 50                                  # 进度条的长度
  4.     total = 100                                 # 总共需要完成的任务数
  5.     progress = 0                                # 当前进度
  6.    
  7.     print('任务开始...')
  8.     while progress < total:
  9.         progress += 1
  10.         percent = progress / total               # 计算当前进度的百分比
  11.         bar = '#' * int(percent * width) + '-' * (width - int(percent * width))   # 根据百分比生成进度条
  12.         
  13.         print('\r[{}] {:.0f}%'.format(bar, percent * 100), end='')   # 通过\r实现覆盖打印
  14.         
  15.         time.sleep(0.1)                         # 模拟任务执行的延迟
  16.         
  17.     print('\n任务完成!')

  18. progress_bar()
复制代码


问题2:每次滚动一直在循环进行,而我的要求是到100就关闭huanYing窗体,然后自动调用main窗体。

可以使用条件判断来控制进度条的循环,并在满足条件时关闭窗体和调用其他窗体。具体代码如下:

  1. import time

  2. def progress_bar():
  3.     width = 50                                  # 进度条的长度
  4.     total = 100                                 # 总共需要完成的任务数
  5.     progress = 0                                # 当前进度
  6.    
  7.     print('任务开始...')
  8.     while progress < total:
  9.         progress += 1
  10.         percent = progress / total               # 计算当前进度的百分比
  11.         bar = '#' * int(percent * width) + '-' * (width - int(percent * width))   # 根据百分比生成进度条
  12.         
  13.         print('\r[{}] {:.0f}%'.format(bar, percent * 100), end='')   # 通过\r实现覆盖打印
  14.         
  15.         time.sleep(0.1)                         # 模拟任务执行的延迟
  16.    
  17.     print('\n任务完成!')
  18.    
  19.     # 关闭huanYing窗体
  20.     # 调用main窗体的代码

  21. progress_bar()
复制代码


在这个例子中,我使用了一个while循环来模拟任务的执行过程。每次循环中,进度条的值都会加1,并计算出当前的百分比和进度条的显示。当进度达到100时,循环结束并输出任务完成的提示信息。你可以根据实际情况将关闭窗体和调用其他窗体的代码添加到适当的位置。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-2-22 15:07:02 | 显示全部楼层    本楼为最佳答案   
问题1:每次滚动的起点数好像是从上次停止的数字开始滚动的,而我的要求是每次启动程序都从0开始滚动的。

可以使用一个变量来记录当前的进度条位置,每次启动程序时将其置为0即可。具体代码如下:

  1. import time

  2. def progress_bar():
  3.     width = 50                                  # 进度条的长度
  4.     total = 100                                 # 总共需要完成的任务数
  5.     progress = 0                                # 当前进度
  6.    
  7.     print('任务开始...')
  8.     while progress < total:
  9.         progress += 1
  10.         percent = progress / total               # 计算当前进度的百分比
  11.         bar = '#' * int(percent * width) + '-' * (width - int(percent * width))   # 根据百分比生成进度条
  12.         
  13.         print('\r[{}] {:.0f}%'.format(bar, percent * 100), end='')   # 通过\r实现覆盖打印
  14.         
  15.         time.sleep(0.1)                         # 模拟任务执行的延迟
  16.         
  17.     print('\n任务完成!')

  18. progress_bar()
复制代码


问题2:每次滚动一直在循环进行,而我的要求是到100就关闭huanYing窗体,然后自动调用main窗体。

可以使用条件判断来控制进度条的循环,并在满足条件时关闭窗体和调用其他窗体。具体代码如下:

  1. import time

  2. def progress_bar():
  3.     width = 50                                  # 进度条的长度
  4.     total = 100                                 # 总共需要完成的任务数
  5.     progress = 0                                # 当前进度
  6.    
  7.     print('任务开始...')
  8.     while progress < total:
  9.         progress += 1
  10.         percent = progress / total               # 计算当前进度的百分比
  11.         bar = '#' * int(percent * width) + '-' * (width - int(percent * width))   # 根据百分比生成进度条
  12.         
  13.         print('\r[{}] {:.0f}%'.format(bar, percent * 100), end='')   # 通过\r实现覆盖打印
  14.         
  15.         time.sleep(0.1)                         # 模拟任务执行的延迟
  16.    
  17.     print('\n任务完成!')
  18.    
  19.     # 关闭huanYing窗体
  20.     # 调用main窗体的代码

  21. progress_bar()
复制代码


在这个例子中,我使用了一个while循环来模拟任务的执行过程。每次循环中,进度条的值都会加1,并计算出当前的百分比和进度条的显示。当进度达到100时,循环结束并输出任务完成的提示信息。你可以根据实际情况将关闭窗体和调用其他窗体的代码添加到适当的位置。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 15:15:18 | 显示全部楼层
FishC 发表于 2024-2-22 15:07
问题1:每次滚动的起点数好像是从上次停止的数字开始滚动的,而我的要求是每次启动程序都从0开始滚动的。
...

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
        self.setValue(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())

请问:1、设置每次都从0开始滚动要在哪里设置?

2、设置到100就自动停止又要在哪里设置?

回复请注意python格式。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 15:15:41 | 显示全部楼层
清風 发表于 2024-02-22 15:15
class CustomProgressBar(QProgressBar):
    def __ ...


1、要在CustomProgressBar类的_startAnimation方法中设置每次都从0开始滚动。可以通过将value属性重置为0来实现。具体代码如下:

  1. def _animate(self):
  2.     value = self.value() + 2
  3.     if value > self.maximum():
  4.         value = value % self.maximum()
  5.     self.setValue(value)
  6.     if value == 0:
  7.         self.timer.stop()
复制代码


2、要在CustomProgressBar类的_startAnimation方法中设置到100就自动停止。可以在value等于0时停止定时器的运行。具体代码如上所示。

完整的修改后的代码如下:

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

  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.setValue(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.         if value == 0:
  37.             self.timer.stop()

  38. class HuanYingWindow(QWidget):
  39.     def __init__(self):
  40.         super().__init__()
  41.         self.initUI()

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

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


在这个例子中,我在CustomProgressBar类的_startAnimation方法中添加了判断value是否等于0的条件来停止定时器的运行。这样,在进度条的滚动过程中,当value回到0时,定时器就会停止,达到设置到100就自动停止的效果。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-22 15:19:09 | 显示全部楼层
FishC 发表于 2024-2-22 15:15
1、要在CustomProgressBar类的_startAnimation方法中设置每次都从0开始滚动。可以通过将value属性重置 ...

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-22 15:19:24 | 显示全部楼层


不客气,希望能够帮助到你!如果还有其他问题,请随时提问。祝你编程顺利!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 17:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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