溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中如何實(shí)現(xiàn)進(jìn)度條progressbar

發(fā)布時(shí)間:2020-06-24 10:54:09 來(lái)源:億速云 閱讀:389 作者:清晨 欄目:編程語(yǔ)言

不懂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)度條:

Python中如何實(shí)現(xiàn)進(jìn)度條progressbar放在循環(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é)果如下:

Python中如何實(shí)現(xiàn)進(jìn)度條progressbar參數(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í)!

向AI問一下細(xì)節(jié)

免責(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)容。

AI