您好,登錄后才能下訂單哦!
不懂Python中如何實(shí)現(xiàn)進(jìn)度條progressbar?其實(shí)想解決這個(gè)問題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
一個(gè)很常用的進(jìn)度條展示小工具——Progressbar。
注:最新版的使用者不要照著GitHub上的文檔去操作,文檔信息沒有維護(hù),會(huì)出現(xiàn)很多問題。
下載模塊
pip install progressbar
注意:安裝模塊的時(shí)候可能會(huì)出現(xiàn)warning,耐心等待,總會(huì)出現(xiàn)success。
簡(jiǎn)單的使用方法
import time from progressbar import * total = 1000 def test_func(): time.sleep(0.01) progress = ProgressBar() for i in progress(range(1000)): test_func()
對(duì)于簡(jiǎn)單的循環(huán)函數(shù)而言,我們只需要把它加在我們組合了progress方法的循環(huán)下就可以了。
我們會(huì)看到如下的進(jìn)度條:
放在循環(huán)外定義使用
# -*- coding=utf-8 -*- from __future__ import division import sys, time from progressbar import * total = 1000 def test_func(): time.sleep(0.01) bar = ProgressBar().start() for i in range(1000): bar.update(int((i / (total - 1)) * 100)) test_func() bar.finish()
注意:不要忽略了start()和fininsh()否則會(huì)出現(xiàn)問題。
多層信息的展示使用
# -*- coding=utf-8 -*- import time from progressbar import * total = 1000 def test_func(): time.sleep(0.01) widgets = [ 'Progress: ', Percentage(), ' ', Bar('#'), ' ', Timer(), ' ', ETA(), ' ', FileTransferSpeed() ] bar = ProgressBar(widgets=widgets, maxval=10 * total).start() for i in range(total): bar.update(10 * i + 1) test_func() bar.finish()
結(jié)果如下:
參數(shù)說(shuō)明:
'Progress: ' :設(shè)置進(jìn)度條前顯示的文字
Percentage() :顯示百分比
Bar('#') :設(shè)置進(jìn)度條形狀
ETA() :顯示預(yù)計(jì)剩余時(shí)間
Timer() :顯示已用時(shí)間
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享Python中如何實(shí)現(xiàn)進(jìn)度條progressbar內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。