简单实现python进度条脚本
最近需要用Python写一个小脚本,用到了一些小知识,赶紧抽空记录一下。不深但是常用。
两个进度条示例,拷贝就能运行:
# 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()提供一个自己写的一个简单异步进度条,可以在耗时操作前开启,然后再耗时操作结束后停止。
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()
相关推荐
  liujia    2020-06-02  
   fgleeldq    2019-12-31  
   qianjq    2019-12-30  
   fengling    2019-12-30  
   playlinuxxx    2011-02-04  
   前端开发Kingcean    2019-10-28  
   Helene    2011-09-17  
   best0power    2015-03-16  
   小傻    2019-07-01  
   莲开十月人思量    2019-06-29  
   nicepainkiller    2019-06-29  
   yqoxygen    2019-06-29  
   椎锋陷陈    2012-06-07  
   Kindlecode    2014-07-07  
   vqudominiapp    2019-06-26  
   renpinghao    2019-06-26  
   永不放弃    2019-06-26  
 