Python使用progressbar模块实现的显示进度条功能

yipeiwu_com5年前Python基础

本文实例讲述了Python使用progressbar模块实现的显示进度条功能。分享给大家供大家参考,具体如下:

progressbar安装:

pip install progressbar

用法一

# -*- coding=utf-8 -*-
import time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
progress = ProgressBar()
for i in progress(range(1000)):
  dosomework()

显示效果:

5% |###                                                                      |
100% |#########################################################################|

用法二

# -*- coding=utf-8 -*-
from __future__ import division
import sys, time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
pbar = ProgressBar().start()
for i in range(1000):
  pbar.update(int((i / (total - 1)) * 100))
  dosomework()
pbar.finish()

显示效果:

39% |##############################                                               |
100% |#############################################################################|

用法三

# -*- coding=utf-8 -*-
import time
from progressbar import *
total = 1000
def dosomework():
  time.sleep(0.01)
widgets = ['Progress: ',Percentage(), ' ', Bar('#'),' ', Timer(),
      ' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=10*total).start()
for i in range(total):
  # do something
  pbar.update(10 * i + 1)
  dosomework()
pbar.finish()

显示效果:

Progress:   3% |###                                                                                | Elapsed Time: 0:00:15 ETA: 0:09:02 919.67  B/s
Progress: 100% |###################################################################################| Elapsed Time: 0:10:10 Time: 0:10:10 917.42  B/s

widgets可选参数含义:

'Progress: ' :设置进度条前显示的文字
Percentage() :显示百分比
Bar('#') : 设置进度条形状
ETA() : 显示预计剩余时间
Timer() :显示已用时间

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python进阶篇之字典操作总结

一、与字典值有关的计算 问题 想对字典的值进行相关计算,例如找出字典里对应值最大(最小)的项。 解决方案一: 假设要从字典 {'a':3, 'b':2, 'c':6} 中找出值最小...

Python 矩阵转置的几种方法小结

我就废话不多说了,直接上代码吧! #Python的matrix转置 matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] def printma...

浅析Python中signal包的使用

在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点在程序中可以用定时器实现这种功能,于是就开始摸索了,发现需要一些信号...

python wxpython 实现界面跳转功能

python wxpython 实现界面跳转功能

用wxpython设计界面时可能会出现界面嵌套的情况 这样就需要进行界面的跳转 但是貌似wxpython没提供界面跳转的方式(也可能是我菜。。。) 所以就需要借助threading模块...

Django models.py应用实现过程详解

Django models.py应用实现过程详解

编写 models.py 文件 from django.db import models # Create your models here. class User_info(mod...