Python之控制台进度条实现
进度条一
源码实现
import sys
import time
def a1():
for i in range(1, 101):
print('\r', end="")
print("进度: {}%: ".format(i), "||" * (i // 2), end="")
sys.stdout.flush()
time.sleep(0.05)
if __name__ == '__main__':
a1()
效果图
进度条二
源码实现
import time
def a2():
scale = 50
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)
if __name__ == '__main__':
a2()
效果图
进度条三
安装模块
pip install tqdm
源码实现
import time
from tqdm import tqdm
def a3():
time.sleep(5)
for i in tqdm(range(1, 101)):
# io的操作
time.sleep(0.1)
time.sleep(1)
if __name__ == '__main__':
a3()
效果图
进度条四
安装模块
pip install progress
源码实现
import time
from progress.bar import Bar
def a4():
b = Bar("下载", max=100)
for item in range(100):
b.next()
time.sleep(1)
b.finish()
if __name__ == '__main__':
a4()
注意事项:
- 在
PyCharm
中的控制台是看不到效果的。只有在cmd
才可以。
效果图
进度条五
安装模块
pip install alive_progress
源码实现
import time
from alive_progress import alive_bar
def a5():
time.sleep(5)
a = range(100)
with alive_bar(len(a)) as bar:
for _ in a:
bar()
time.sleep(0.3)
if __name__ == '__main__':
a5()
效果图
可视化进度条
安装模块
pip install PySimpleGUI
源码实现
import time
import PySimpleGUI as ui
def a6():
a = [i for i in range(1, 9)]
for i, item in enumerate(a):
ui.one_line_progress_meter("下载", i + 1, len(a), '下载')
time.sleep(1)
if __name__ == '__main__':
a6()