
如何在 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. 富文本进度条
富文本进度条提供更多自定义选项,例如更改文本颜色和样式。
- 安装: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终端进度条 python终端显示进度条百分比的详细内容,更多请关注知识资源分享宝库其它相关文章!







发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。