向python函数添加进度条 [英] Add progress bar to a python function

查看:58
本文介绍了向python函数添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢对初学者的任何帮助:) 我尝试了以下方法但不确定如何包装 def Job():

Appreciate any help for a beginner :) I tried the below but not sure how to wrap the def Job():

import time
from progressbar import ProgressBar


pbar = ProgressBar()
def job():
        Script ....
        Script ...
        Script ...
        Script ...

推荐答案

你可以这样使用 bar 对象:

You can use the bar object this way:

import time
import progressbar


def job():
    bar = progressbar.ProgressBar()
    for i in bar(range(100)):
        ... # Code that you want to run
        #time.sleep(0.02)

job()

如果你要执行的代码执行时间很快,你可以在里面放一个time.sleep(),以免在开始时progressbar设置为100%.

If the code you want to execute has fast execution time, you can put a time.sleep() inside in order not to have progressbar set to 100% in the beginning.

这篇关于向python函数添加进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆