参考官方文档:https://pypi.org/project/progressbar/#description
- progressbar 安装:
pip install progressbar
- progressbar.ProgressBar() 可选参数:
maxval = None widgets = None term_width = None poll:int = 1 left_justify:bool = True fd = None 例: progressbar.ProgressBar(widgets=widgets, maxval)
- widgets 可选参数:
'Progress: ' :设置进度条前显示的文字 Percentage() :显示百分比 Bar('#') : 设置进度条形状 ETA() : 显示预计剩余时间 Timer() :显示已用时间 FileTransferSpeed() :显示传输速度 例: widgets = ['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ',FileTransferSpeed()] bar = ProgressBar(widgets=widgets, maxval=total)
#写法1
import time,progressbar dowork=lambda:time.sleep(0.01) bar = progressbar.ProgressBar() for i in bar(range(1000)): dowork()
#写法2
import time,progressbar dowork=lambda:time.sleep(0.01) bar = progressbar.ProgressBar() bar.start() for i in range(100): bar.update(i+1) #int((i / (total - 1)) * 100) dowork() bar.finish()#注意,一定要加这句话
例:
import time from progressbar import * total = 100 dowork=lambda:time.sleep(0.01) widgets = ['Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ',FileTransferSpeed()] bar = ProgressBar(widgets=widgets, maxval=total) bar.start()# for i in range(total): bar.update(i+1) dowork() bar.finish()