卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章75059本站已运行4311

python终端进度条 python终端显示进度条百分比

在 python 终端显示进度条有三种方式:tqdm 库:安装 pip install tqdm,循环任务时更新进度条。富文本进度条:安装 pip install rich,提供更多自定义选项。自制进度条:编写简单代码,打印完成百分比进度条。

python终端进度条 python终端显示进度条百分比

如何在 Python 终端显示进度条

显示进度条是向用户提供有关正在进行任务的反馈的一种有效方法。在 Python 终端中,可以通过使用以下方法来实现:

1. tqdm 库

  • 安装:pip install tqdm
  • 用法:
import tqdm

# 创建一个进度条
progress_bar = tqdm.tqdm(total=100)

# 循环任务
for i in range(100):
    # 更新进度条
    progress_bar.update(1)

2. 富文本进度条

立即学习“Python免费学习笔记(深入)”;

富文本进度条提供更多自定义选项,例如更改文本颜色和样式。

  • 安装:pip install rich
  • 用法:
from rich.progress import Progress

# 创建一个进度条
progress = Progress()

# 循环任务
for i in range(100):
    # 更新进度条
    progress.update(progress=i / 100, refresh=True)

3. 自制进度条

也可以编写一个简单的自制进度条来显示任务的完成百分比:

def progress_bar(current, total):
    """打印一个简单的进度条"""
    percent_complete = current / total
    progress_bar = "#" * int(percent_complete * 50)
    print(progress_bar, end="r")

# 循环任务
for i in range(100):
    # 更新进度条
    progress_bar(i, 100)
卓越飞翔博客
上一篇: Python控制台显示动态进度条
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏