Python-print骚操作之进度条(一)

目录

print之flush参数

tqdm

yaspin


print之flush参数

flush 默认为 False,只有所有内容都有了, 然后一次性都打印出来,使用 True 就可以做到 每次打印都及时显示出来,使用是需要配合end参数

import time, warnings
for i in range(100):
    if i == 0:
        print(' 开始下载')
    print('\r', '正在下载中...已下载{}%'.format(i + 1), end='', flush=True)
    if i == 99:
        print('\n 完成下载')
    time.sleep(.1)

tqdm

# coding=utf-8
import time, warnings
from tqdm import tqdm, trange
for i in trange(3, desc='总进度'):
    for j in trange(50, desc='子任务进度'):
        time.sleep(0.1)

import time, warnings
from tqdm import tqdm

for i in tqdm(range(100), ncols=80, desc='正在下载中', mininterval=.2, miniters=5, colour='green'):
    # ncols进度条的宽度
    # nrows进度条的高度
    # desc描述词
    # mininterval最小进度显示更新间隔[默认:0.1]秒
    # miniters进度条最小进度显示更新间隔
    # colour进度条颜色
    time.sleep(.05)

yaspin

import warnings, time
from yaspin import yaspin
from yaspin.spinners import Spinners

warnings.filterwarnings('ignore')
with yaspin(text='音乐下载中...', side='right', color='green') as yp:  # 修改位置side及颜色color
    music = ['可可托海的牧羊人', '黄昏', '凉凉', '兰亭集']
    for mc in music:
        time.sleep(2)
        yp.write('%s已完成下载' % mc)
    yp.text = '全部下载完成'
    yp.ok('√')

乒乓球

with yaspin(text='音乐下载中...').yellow.bold.underline.pong.on_green as yp:  # 乒乓球
    music = ['可可托海的牧羊人', '黄昏', '凉凉', '兰亭集']
    for mc in music:
        time.sleep(2)
        yp.write('%s已完成下载' % mc)

  

召唤鲨鱼

with yaspin().white.bold.shark.on_yellow as yp:  # 召唤鲨鱼
    yp.text = '音乐下载中...'
    music = ['可可托海的牧羊人', '黄昏', '凉凉', '兰亭集']
    for mc in music:
        time.sleep(2)
        yp.write('%s完成下载' % mc)

 

弹跳球,自定义颜色效果

with yaspin(color="green", on_color='on_white', attrs=["dark", "blink", 'concealed']).bouncingBall as yp:  # 自定义颜色
    yp.text = '正在加载中,请稍等...'
    time.sleep(5)

  

地球/月球

import warnings, time
from yaspin import yaspin
from yaspin.spinners import Spinners

warnings.filterwarnings('ignore')
with yaspin(Spinners.earth, text="Earth") as yp:
    time.sleep(2)
    yp.spinner = Spinners.moon
    yp.text = "Moon"
    time.sleep(2)
    yp.write('从地球变为月球结束')  # 当微调器打开时,不应使用print在终端中编写任何消息。 要在终端中写入消息而不与yaspin微调器发生任何冲突,将提供一个.write()方法

中文文档:Python yaspin包_程序模块 - PyPI - Python中文网 (cnpython.com)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值