首页 > 文章列表 > python自定义进度条显示信息

python自定义进度条显示信息

Python
315 2022-08-07

1、通过update方法可以控制每次进度条更新的进度。

import time
from tqdm import tqdm
 
#  total参数设置进度条的总长度为100
with tqdm(total=100) as pbar:
    for i in range(100):
        time.sleep(0.1)
        #  每次更新进度条的长度为1
        pbar.update(1)

2、通过set_description和set_postfix方法设置进度条显示信息。将进度条显示的信息设定为中文时,不会出现乱码。

将进度条显示的信息设定为中文时,不会出现乱码。
import time
from tqdm import trange
from random import random, randint
 
with trange(100) as t:
    for i in t:
        #  设置进度条左边显示的信息
        #  注意:代码中的GEN是可以手动换成其它内容的
        t.set_description("GEN %i" % i)
        #  设置进度条右边显示的信息
        #  注意:此处代码中的gen lr lst是可以手动换成其它内容的
        t.set_postfix(loss=random(), gen=randint(1, 999), lr="h", lst=[1, 2])
        time.sleep(0.1)

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。