昨天突然想到想要自己制作一个属于自己的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)
最终运行结果如下:
码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~