Python制作进度条的几种方法
作者:宋宋讲编程 时间:2022-03-11 05:36:23
前言
如果你之前没用过进度条,八成是觉得它会增加不必要的复杂性或者很难维护,其实不然。要加一个进度条其实只需要几行代码。
from alive_progress import alive_bar; import time
在这几行代码中,我们可以看看如何在命令行脚本以及 PySimpleGUI UI 中添加进度条。
下文将介绍 4 个常用的 Python 进度条库:
Progress
第一个要介绍的 Python 库是 Progress。
你只需要定义迭代的次数、进度条类型并在每次迭代时告知进度条。
import timefrom progress.bar
import IncrementalBarmylist = [1,2,3,4,5,6,7,8]
bar = IncrementalBar( Countdown , max = len(mylist))
for item in mylist:
bar.next()
time.sleep(1)
bar.finish()
Progress 实现的进度条效果如下:
Progressbar 的增量进度条
如果你不喜欢该进度条的格式,还可以从以下格式中挑选:
ProgressBar 支持的进度条类型。
相关文档:https://pypi.org/project/progress/1.5/
tqdm
下面我们看一下 tqdm 库。
和之前见过的库差不多,这两行代码也非常相似,在设置方面有一点点不同:
import timefrom tqdm
import tqdmmylist = [1,2,3,4,5,6,7,8]
for i in tqdm(mylist):
time.sleep(1)
tqdm 实现的进度条效果如下:
这个进度条也提供了几种选项。
相关文档:https://tqdm.github.io/
Alive Progress
顾名思义,这个库可以使得进度条变得生动起来,它比原来我们见过的进度条多了一些动画效果。
从代码角度来说比较相似:
from alive_progress import alive_barimport
timemylist = [1,2,3,4,5,6,7,8]
with alive_bar(len(mylist)) as bar:
for i in mylist:
bar()
time.sleep(1)
进度条的外观和预期差不多:
这种进度条有一些与众不同的功能,使用起来会比较有趣,功能详情可见项目:https://github.com/rsalmei/alive-progress
PySimpleGUI
用 PySimpleGUI 得到图形化进度条
我们可以加一行简单的代码,在命令行脚本中得到图形化进度条。
为了实现上述内容,我们需要的代码是:
import PySimpleGUI as sgimport
timemylist = [1,2,3,4,5,6,7,8]
for i, item in enumerate(mylist):
sg.one_line_progress_meter( This is my progress meter! , i+1, len(mylist), -key- )
time.sleep(1)
PySimpleGUI 应用程序中的进度条项目作者之前曾经在 GitHub 上讨论过「如何快速启动 Python UI,然后使用 UI 创建比较工具」。
在这个项目里,作者还讨论了一下如何集成进度条。
代码如下:
import PySimpleGUI as sgimport
timemylist = [1,2,3,4,5,6,7,8]
progressbar = [ [sg.ProgressBar(len(mylist), orientation= h , size=(51, 10), key= progressbar )]]
outputwin = [ [sg.Output(size=(78,20))]]
layout = [ [sg.Frame( Progress ,layout= progressbar)], [sg.Frame( Output , layout = outputwin)], [sg.Submit( Start ),sg.Cancel()]]
window = sg.Window( Custom Progress Meter , layout)
progress_bar = window[ progressbar ] while True:
event, values = window.read(timeout=10)
if event == Cancel or event is None:
break
elif event == Start :
for i,item in enumerate(mylist):
print(item)
time.sleep(1)
progress_bar.UpdateBar(i + 1)window.close()
没错,在 Python 脚本中使用进度条只需要几行代码,一点也不复杂。有了进度条,以后也不用再猜测脚本运行的怎么样了。
来源:https://blog.csdn.net/qiqi1220/article/details/128220243
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
将HTML自动转为JS代码
matplotlib绘制符合论文要求的图片实例(必看篇)
![](https://img.aspxhome.com/file/2023/2/81872_0s.png)
Python文件读写open函数详解
![](https://img.aspxhome.com/file/2023/1/110001_0s.png)
Python基础 while循环与break、continue关键字
![](https://img.aspxhome.com/file/2023/3/64283_0s.jpg)
python将ip地址转换成整数的方法
关于使用python对mongo多线程更新数据
关于Dreamweaver乱码问题的解决方案
Python模块汇总(常用第三方库)
![](https://img.aspxhome.com/file/2023/4/91864_0s.png)
树莓派用python中的OpenCV输出USB摄像头画面
![](https://img.aspxhome.com/file/2023/1/85271_0s.jpg)
详解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和区别
Python小游戏之300行代码实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/1/108121_0s.png)
Python获取时间范围内日期列表和周列表的函数
![](https://img.aspxhome.com/file/2023/8/75928_0s.png)
Python实现清除文件夹中重复视频
![](https://img.aspxhome.com/file/2023/3/61183_0s.jpg)
在MAC上搭建python数据分析开发环境
Windows64x下VScode下载过程
![](https://img.aspxhome.com/file/2023/1/81441_0s.jpg)
Django ORM 查询表中某列字段值的方法
Python之根据输入参数计算结果案例讲解
![](https://img.aspxhome.com/file/2023/9/69499_0s.jpg)
简述python四种分词工具,盘点哪个更好用?
Python实现PS滤镜中马赛克效果示例
![](https://img.aspxhome.com/file/2023/1/91871_0s.jpg)