本文實例講述了python在控制台輸出進度條的方法。分享給大家供大家參考。具體實現方法如下:
進度條效果如下所示:
?
1 2 |#############################---------------------| 59 percent done代碼如下:
?
1 2 3 4 5 6 7 8 9 class ProgressBar(): def __init__(self, width=50): self.pointer = 0 self.width = width def __call__(self,x): # x in percent self.pointer = int(self.width*(x/100.0)) return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+ "|n %d percent done" % int(x)Test function (for windows system, change "clear" into "CLS"):
?
1 2 3 4 5 6 7 if __name__ == '__main__': import time, os pb = ProgressBar() for i in range(101): os.system('clear') print pb(i) time.sleep(0.1)希望本文所述對大家的Python程序設計有所幫助。