脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python在控制台输出进度条的方法

python在控制台输出进度条的方法

2020-07-16 10:43不吃皮蛋 Python

这篇文章主要介绍了python在控制台输出进度条的方法,实例分析了Python输出进度条效果的方法,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

延伸 · 阅读

精彩推荐