你的Python程序需要进度条

  • 生活中很多事情都会有反馈机制,比如考试完,成绩就是对这段时间学习效果的反馈;玩游戏经验值就是对你在游戏中投入精力的反馈等等。那么对于程序来说也需要反馈,比如安装一个软件,如果没有进度条,那么你一定会很抓狂。因为你根本无法确认程序是否在正常工作,还是任务已经被已系统挂起。那么本文就简单的介绍几种python常用到的反馈进度条方式(包括GUI进度条)

本文首发于伊洛的个人博客:https://yiluotalk.com,欢迎关注并查看更多内容!!!

1. 使用 Progress
  • 安装
(yiluo) ➜  ~ pip install progress
Collecting progress
  Downloading https://files.pythonhosted.org/packages/38/ef/2e887b3d2b248916fc2121889ce68af8a16aaddbe82f9ae6533c24ff0d2b/progress-1.5.tar.gz
Installing collected packages: progress
    Running setup.py install for progress ... done
Successfully installed progress-1.5
  • 创建一个脚本,写入以下代码
# 伊洛Yiluo 
# https://yiluotalk.com

import time
from progress.bar import IncrementalBar

bar_list = [1, 2, 3, 4, 5, 6, 7, 8]

bar = IncrementalBar('progress bar', max=len(bar_list))


if __name__ == '__main__':
    for i in bar_list:
        bar.next()
        time.sleep(1)

    bar.finish()
  • 命令行运行
(yiluo) ➜  AllDemo python demo.py
progress bar |████████████                    | 3/8

2. 使用 Tqdm
  • 安装
pip install tqdm
Collecting tqdm
  Downloading https://files.pythonhosted.org/packages/8c/c3/d049cf3fb31094ee045ec1ee29fffac218c91e82c8838c49ab4c3e52627b/tqdm-4.41.0-py2.py3-none-any.whl (56kB)
     |████████████████████████████████| 61kB 239kB/s
Installing collected packages: tqdm
Successfully installed tqdm-4.41.0
  • 写入以下代码
# 伊洛Yiluo
# https://yiluotalk.com

import time
from tqdm import tqdm

_bar = [1, 2, 3, 4, 5, 6, 7, 8]


for item in tqdm(_bar):
    time.sleep(1)
  • 命令行运行
(yiluo) ➜  AllDemo python demo.py
 62%|█████████████████████████████████▊                    | 5/8 
3. 使用 Alive Progress
  • 也是很常用的,和之前例子相差不大,有兴趣的可以看以下具体的使用文档
    Alive Progress 传送门
4. 使用 Pysimplegui(GUI)
  • 安装
(yiluo) ➜  ~ pip install pysimplegui
Collecting pysimplegui
  Downloading https://files.pythonhosted.org/packages/22/a8/ec06b5ce8997411c542dc0f65848a89b6f852b1b9c0fde8ace89aec6703e/PySimpleGUI-4.14.1-py3-none-any.whl (288kB)
     |████████████████████████████████| 296kB 286kB/s
Installing collected packages: pysimplegui
Successfully installed pysimplegui-4.14.1
# 伊洛Yiluo
# https://yiluotalk.com

import PySimpleGUI as sg
import time
bar_list = [1, 2, 3, 4, 5, 6, 7, 8]

for i, item in enumerate(bar_list):
    sg.one_line_progress_meter('我是进度条!', i+1, len(bar_list), 'key')
    time.sleep(1)
  • 来看看GUI的效果


欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

你可能感兴趣的:(你的Python程序需要进度条)