進(jìn)度條比較常見于程序安裝澡为、文件拷貝诺祸、系統(tǒng)編譯等地方渐扮,在python中有很多庫可以實現(xiàn)進(jìn)度條,也可以自己實現(xiàn)進(jìn)度條
進(jìn)度條相關(guān)的模塊
progress模塊
progress模塊里面實現(xiàn)了多種進(jìn)度條樣式
- Bar
- ChargingBar
- FillingSquaresBar
- FillingCirclesBar
- IncrementalBar
- PixelBar
- ShadyBar
每種進(jìn)度條樣式的使用方法都類似方椎,下面給出兩個實例
import time
from progress.bar import (IncrementalBar,ChargingBar, FillingSquaresBar)
def incrementalBar_progress():
mylist = [1, 2, 3, 4, 5, 6, 7, 8]
bar = IncrementalBar("進(jìn)度條", max = len(mylist))
for item in mylist:
bar.next()
time.sleep(0.1)
bar.finish()
def chargingBar_progress():
bar = ChargingBar("進(jìn)度條1", max = 100)
for item in range(100):
bar.next()
time.sleep(0.1)
bar.finish()
if __name__ == '__main__':
incrementalBar_progress()
chargingBar_progress()
輸出如下
進(jìn)度條_progress
progressbar模塊
實例
import time
import progressbar
def progressbar_progress():
p = progressbar.ProgressBar()
for i in p(range(100)):
'''code'''
time.sleep(0.05)
if __name__ == '__main__':
progressbar_progress()
顯示效果如下
progressbar
alive_progress模塊
實例
import time
from alive_progress import alive_bar
def alive_progress():
with alive_bar(len(range(100))) as bar:
for item in range(100):
bar()
'''code'''
time.sleep(0.05)
if __name__ == '__main__':
alive_progress()
顯示效果如下
alive_progress
tqdm模塊
實例
import time
from tqdm import tqdm
def tqdm_progress():
for i in tqdm(range(1, 60)):
''' 假設(shè)code執(zhí)行0.1s '''
time.sleep(0.1)
if __name__ == '__main__':
tqdm_progress()
顯示效果如下
tqdm
不使用已有模塊實現(xiàn)進(jìn)度條
在網(wǎng)上看到的兩個例子,搬運過來以做學(xué)習(xí)钧嘶,感謝網(wǎng)友的分享
import sys
import time
def normal_progress():
for i in range(1, 101):
print("\r", end = "")
print("進(jìn)度:{}%:".format(i), "#"*(i//2), end="")
sys.stdout.flush()
time.sleep(0.05)
print("")
def normal_progress_time():
t = 60
print("*****帶時間的進(jìn)度條****")
start = time.perf_counter()
for i in range(t + 1):
finsh = "#" * i
need_do = "_" * (t - i)
progress = (i/t)*100
dur = time.perf_counter() - start
print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(progress, finsh, need_do, dur), end="")
time.sleep(0.05)
print("")
if __name__ == '__main__':
normal_progress()
normal_progress_time()
顯示效果如下
normal_progress
normal_progress_time