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


Posted in Python onMay 31, 2018

本文实例讲述了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中使用装饰器和元编程实现结构体类实例
Jan 28 Python
python利用dir函数查看类中所有成员函数示例代码
Sep 08 Python
python实现百度语音识别api
Apr 10 Python
python 读取DICOM头文件的实例
May 07 Python
可能是最全面的 Python 字符串拼接总结【收藏】
Jul 09 Python
python set内置函数的具体使用
Jul 02 Python
pytorch: Parameter 的数据结构实例
Dec 31 Python
python itsdangerous模块的具体使用方法
Feb 17 Python
Python中的None与 NULL(即空字符)的区别详解
Sep 24 Python
Python虚拟环境virtualenv是如何使用的
Jun 20 Python
python not运算符的实例用法
Jun 30 Python
人工智能深度学习OpenAI baselines的使用方法
May 20 Python
python调用Matplotlib绘制分布点并且添加标签
May 31 #Python
python批量修改文件编码格式的方法
May 31 #Python
Python用for循环实现九九乘法表
May 31 #Python
python实现txt文件格式转换为arff格式
May 31 #Python
从django的中间件直接返回请求的方法
May 30 #Python
Django项目中包含多个应用时对url的配置方法
May 30 #Python
django 多数据库配置教程
May 30 #Python
You might like
自制汽车收音机天线:收听广播的技巧和方法
2021/03/02 无线电
phpmailer发送gmail邮件实例详解
2013/06/24 PHP
php实现mysql数据库连接操作及用户管理
2015/11/08 PHP
php 提交表单 关闭layer弹窗iframe的实例讲解
2018/08/20 PHP
PHP convert_uudecode()函数讲解
2019/02/14 PHP
PHP使用 Pear 进行安装和卸载包的方法详解
2019/07/08 PHP
PHP+MySQL实现在线测试答题实例
2020/01/02 PHP
基于Jquery的回车成tab焦点切换效果代码(Enter To Tab )
2010/11/14 Javascript
ParseInt函数参数设置介绍
2014/01/02 Javascript
asp.net刷新本页面的六种方法总结
2014/01/07 Javascript
Javascript排序算法之合并排序(归并排序)的2个例子
2014/04/04 Javascript
原生js和jquery实现图片轮播特效
2015/04/23 Javascript
Bootstrap弹出带合法性检查的登录框实例代码【推荐】
2016/06/23 Javascript
JS实现六位字符密码输入器功能
2016/08/19 Javascript
AngularJs表单验证实例代码解析
2016/11/29 Javascript
js实现带缓动动画的导航栏效果
2017/01/16 Javascript
微信小程序 ecshop地址三级联动实现实例代码
2017/02/28 Javascript
JS实现json的序列化和反序列化功能示例
2017/06/13 Javascript
django之session与分页(实例讲解)
2017/11/13 Python
利用python GDAL库读写geotiff格式的遥感影像方法
2018/11/29 Python
Python3模拟登录操作实例分析
2019/03/12 Python
Pandas读写CSV文件的方法示例
2019/03/27 Python
简单了解python高阶函数map/reduce
2019/06/28 Python
python使用Pandas库提升项目的运行速度过程详解
2019/07/12 Python
django之自定义软删除Model的方法
2019/08/14 Python
tensorflow 分类损失函数使用小记
2020/02/18 Python
Python+Xlwings 删除Excel的行和列
2020/12/19 Python
用Python自动清理系统垃圾的实现
2021/01/18 Python
出纳岗位职责
2013/11/09 职场文书
自我介绍演讲稿
2014/01/15 职场文书
幼教简历自我评价
2014/01/28 职场文书
知识竞赛拉拉队口号
2014/06/16 职场文书
电教室标语
2014/06/20 职场文书
税务干部个人整改措施思想汇报
2014/10/10 职场文书
美丽人生观后感
2015/06/03 职场文书
不要在HTML中滥用div
2021/05/08 HTML / CSS