python使用progressbar顯示進度條


progressbar安裝:

pip install progressbar


用法一

# -*- coding=utf-8 -*-

import time
from progressbar import *

total = 1000

def dosomework():
    time.sleep(0.01)

progress = ProgressBar()
for i in progress(range(1000)):
    dosomework()

顯示效果:

5% |###                                                                      |
100% |#########################################################################|

用法二

# -*- coding=utf-8 -*-

from __future__ import division

import sys, time
from progressbar import *

total = 1000

def dosomework():
    time.sleep(0.01)

pbar = ProgressBar().start()
for i in range(1000):
    pbar.update(int((i / (total - 1)) * 100))
    dosomework()
pbar.finish()
顯示效果:
39% |##############################                                               |
100% |#############################################################################|


用法三

# -*- coding=utf-8 -*-

import  time
from progressbar import *

total = 1000

def dosomework():
    time.sleep(0.01)

widgets = ['Progress: ',Percentage(), ' ', Bar('#'),' ', Timer(),
           ' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=10*total).start()
for i in range(total):
    # do something
    pbar.update(10 * i + 1)
    dosomework()
pbar.finish()

顯示效果:

Progress:   3% |###                                                                                | Elapsed Time: 0:00:15 ETA: 0:09:02 919.67  B/s
Progress: 100% |###################################################################################| Elapsed Time: 0:10:10 Time: 0:10:10 917.42  B/s

widgets可選參數含義:

  • 'Progress: ' :設置進度條前顯示的文字
  • Percentage() :顯示百分比
  • Bar('#') : 設置進度條形狀
  • ETA() : 顯示預計剩余時間
  • Timer() :顯示已用時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM