- 生活中加载网页时、安装程序时会经常见到进度读条
- So 如何实现呢?
- 以下代码即可实现读条功能:
import time for i in range(0,101,2): time.sleep(0.11) # 控制读条速度 char_num = i//2 # 打印多少个'■' per_str = ' %s%% : %s ' % (i, '■' * char_num) if i == 100 else ' %s%% : %s'%(i,'■'*char_num) print(per_str,end='', flush=True)
等价于:
import time for i in range(0,101,2): time.sleep(0.11) # 控制读条速度 char_num = i//2 # 打印多少个'■' if i == 100: per_str = ' %s%% : %s ' % (i, '■' * char_num) else: per_str = ' %s%% : %s'%(i,'■'*char_num) print(per_str,end='', flush=True)
执行结果:
100% : ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Process finished with exit code 0
It's Funny ! enhn~~~~