python 如何实现在控制台界面输出动态进度条

mac2025-12-07  9

import sys import time import random import threading pro = 0 i = 0 timeout = 20 def progress_1(): global pro k = 1 while True: if k % 2 == 0: print("\r%s%s[/][%d]%%" % (">" * pro, " " * (100 - pro), pro), end='') else: print("\r%s%s[\][%d]%%" % (">" * pro, " " * (100 - pro), pro), end='') if pro > 100: break k += 1 time.sleep(0.5) def progress_2(): global i global pro global timeout while i <= timeout: pro = i * 100 // timeout time.sleep(random.random() * 5) i += 1 t1 = threading.Thread(target=progress_1) t2 = threading.Thread(target=progress_2) t2.start() t1.start()
最新回复(0)