Python 实现进度条
1、案例一
代码
import sys
import time
def progress_bar():
for i in range(1, 101):
print("\r", end="")
print("Download progress: {}%: ".format(i), "▋" * (i // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
if __name__ == '__main__':
progress_bar()
结果
2、案例二
代码
import time
scale = 50
print("执行开始,祈祷不报错".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
time.sleep(0.1)
print("\n" + "执行结束,万幸".center(scale // 2, "-"))
结果
3、案例三
安装
pip install tqdm
代码
from time import sleep
from tqdm import tqdm
# 这里同样的,tqdm就是这个进度条最常用的一个方法
# 里面存一个可迭代对象
for i in tqdm(range(1, 500)):
# 模拟你的任务
sleep(0.01)
sleep(0.5)
结果
4、案例四
代码
import PySimpleGUI as sg
import time
mylist = [i for i in range(1000)]
for i, item in enumerate(mylist):
sg.one_line_progress_meter('This is my progress meter!', i + 1, len(mylist), '-key-')
time.sleep(0.1)
结果