Python 自制简易ProgressBar

昨天突然想到想要自己制作一个属于自己的ProgressBar。但是不知道怎么操作,今天想了想有了一个简单的雏形,后面有时间了会进一步更新,欢迎大家一起讨论。
代码如下:

import time


class ProgressBar:
    def __init__(self):
        self.whole_percent = 100  # in progress, the whole percent is 100%

    def progress(self, total, name="Progress"):
        print(f"{name} :|", end="")
        step = total / self.whole_percent
        for i in range(10):
            num = int(step * 100)  # each second the increase number of # sign
            time.sleep(1)
            print("#" * num, end="")
        print("|")


if __name__ == '__main__':
    main = ProgressBar()
    time_ = 10
    main.progress(total=10)

最终运行结果如下:
在这里插入图片描述

然而这样的结果并不是很令人满意,我们可以进一步执行下面的策略。在阅读如下代码前,推荐阅读Python 中的\r 字符(超链接点击跳转)。

import time


class ProgressBar:
    def __init__(self):
        self.whole_percent = 100  # in progress, the whole percent is 100%

    def progress(self, total, name="Progress"):
        step = total / self.whole_percent
        for i in range(total + 1):
            num = round(step * self.whole_percent)  # each second the increase number of # sign
            num_hashtag = num * i
            rest = self.whole_percent - 1 - num_hashtag
            time.sleep(1)
            print(f"\r{name} :|{'#' * num_hashtag}{' ' * rest} |{round(step * i * 100, 3)}/{self.whole_percent}.0", end="")


if __name__ == '__main__':
    main = ProgressBar()
    time_ = 10
    main.progress(total=10)

最终运行结果如下:
在这里插入图片描述

码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勤奋的大熊猫

你的鼓励将是我写作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值