詳解Python實(shí)現(xiàn)進(jìn)度條的4種方式
這里只列舉了部分方法,其他方法或python庫暫時(shí)還沒使用到
1.不用庫,直接打?。?/strong>
代碼樣例:
import time #demo1 def process_bar(percent, start_str='', end_str='', total_length=0): bar = ''.join(["\033[31m%s\033[0m"%' '] * int(percent * total_length)) + '' bar = '\r' + start_str + bar.ljust(total_length) + ' {:0>4.1f}%|'.format(percent*100) + end_str print(bar, end='', flush=True) for i in range(101): time.sleep(0.1) end_str = '100%' process_bar(i/100, start_str='', end_str=end_str, total_length=15) #demo2 # for i in range(0, 101, 2): # time.sleep(0.1) # num = i // 2 # if i == 100: # process = "\r[%3s%%]: |%-50s|\n" % (i, '|' * num) # else: # process = "\r[%3s%%]: |%-50s|" % (i, '|' * num) # print(process, end='', flush=True)
效果:
2.不用庫,直接打印:
代碼樣例:
import sys, time print("正在下載......") for i in range(11): if i != 10: sys.stdout.write("==") else: sys.stdout.write("== " + str(i*10)+"%/100%") sys.stdout.flush() time.sleep(0.2) print("\n" + "下載完成")
注:
sys.stdout.write()方法跟print()方法的區(qū)別是 前者打印不換行,后者換行。
sys.stdout.flush()方法是立即刷新輸出的內(nèi)容
效果:
3.tqdm庫
相關(guān)文檔:點(diǎn)擊直達(dá)
使用樣例:
from time import sleep from tqdm import tqdm for i in tqdm(range(20)): sleep(0.5)
效果:
4.progressbar
官方文檔:點(diǎn)擊直達(dá)
安裝:
pip3 install progressbar2
使用樣例:
import time from progressbar import * progress = ProgressBar() for i in progress(range(1000)): time.sleep(0.01)
效果:
總結(jié)
以上所述是小編給大家介紹的Python實(shí)現(xiàn)進(jìn)度條的4種方式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- python tqdm實(shí)現(xiàn)進(jìn)度條的示例代碼
- python進(jìn)度條顯示-tqmd模塊的實(shí)現(xiàn)示例
- python進(jìn)度條顯示之tqmd模塊
- Python 給下載文件顯示進(jìn)度條和下載時(shí)間的實(shí)現(xiàn)
- python動(dòng)態(tài)文本進(jìn)度條的實(shí)例代碼
- python如何通過pyqt5實(shí)現(xiàn)進(jìn)度條
- 6行Python代碼實(shí)現(xiàn)進(jìn)度條效果(Progress、tqdm、alive-progress和PySimpleGUI庫)
- wxPython實(shí)現(xiàn)帶顏色的進(jìn)度條
- Python進(jìn)度條的制作代碼實(shí)例
- Python Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn)
- Python 實(shí)現(xiàn)進(jìn)度條的六種方式
相關(guān)文章
Python之Anaconda啟動(dòng)過程中的異常錯(cuò)誤問題及解決
這篇文章主要介紹了Python之Anaconda啟動(dòng)過程中的異常錯(cuò)誤問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09使用 Supervisor 監(jiān)控 Python3 進(jìn)程方式
今天小編就為大家分享一篇使用 Supervisor 監(jiān)控 Python3 進(jìn)程方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python Web開發(fā)你要理解的WSGI & uwsgi詳解
這篇文章主要給大家介紹了關(guān)于python Web開發(fā)你一定要理解的WSGI & uwsgi的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08pycharm中使用pyplot時(shí)報(bào)錯(cuò)MatplotlibDeprecationWarning
最近在使用Pycharm中matplotlib作圖處理時(shí)報(bào)錯(cuò),所以這篇文章主要給大家介紹了關(guān)于pycharm中使用pyplot時(shí)報(bào)錯(cuò)MatplotlibDeprecationWarning的相關(guān)資料,需要的朋友可以參考下2023-12-12python執(zhí)行子進(jìn)程實(shí)現(xiàn)進(jìn)程間通信的方法
這篇文章主要介紹了python執(zhí)行子進(jìn)程實(shí)現(xiàn)進(jìn)程間通信的方法,涉及Python使用subprocess模塊操作進(jìn)程的相關(guān)技巧,需要的朋友可以參考下2015-06-06