簡單實現(xiàn)python進(jìn)度條腳本
更新時間:2017年12月18日 12:07:31 作者:I-Awakening
這篇文章主要教大家如何簡單實現(xiàn)python進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
最近需要用Python寫一個小腳本,用到了一些小知識,趕緊抽空記錄一下。不深但是常用。
兩個進(jìn)度條示例,拷貝就能運行:
# coding=utf-8 import sys import time # width:寬度, percent:百分比 def progress(width, percent): print "\r%s %d%%" % (('%%-%ds' % width) % (width * percent / 100 * '='), percent), if percent >= 100: print sys.stdout.flush() # 示例一、0%--100% def demo1(): for i in xrange(100): progress(50, (i + 1)) time.sleep(0.1) ## 示例二、周期加載 def demo2(): i = 19 n = 200 while n > 0: print "\t\t\t%s \r" % (i * "="), i = (i + 1) % 20 time.sleep(0.1) n -= 1 demo1() demo2()
提供一個自己寫的一個簡單異步進(jìn)度條,可以在耗時操作前開啟,然后再耗時操作結(jié)束后停止。
import time import thread import sys class Progress: def __init__(self): self._flag = False def timer(self): i = 19 while self._flag: print "\t\t\t%s \r" % (i * "="), sys.stdout.flush() i = (i + 1) % 20 time.sleep(0.05) print "\t\t\t%s\n" % (19 * "="), thread.exit_thread() def start(self): self._flag = True thread.start_new_thread(self.timer, ()) def stop(self): self._flag = False time.sleep(1)
用法:
progress = Progress() progress.start() time.sleep(5) progress.stop()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用itchat模塊實現(xiàn)群聊轉(zhuǎn)發(fā),自動回復(fù)功能示例
這篇文章主要介紹了Python使用itchat模塊實現(xiàn)群聊轉(zhuǎn)發(fā),自動回復(fù)功能,結(jié)合實例形式分析了Python基于itchat模塊針對微信信息的發(fā)送、回復(fù)等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08Python 跨.py文件調(diào)用自定義函數(shù)說明
這篇文章主要介紹了Python 跨.py文件調(diào)用自定義函數(shù)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06