Python-实现进度条的6种方式

Python 实现进度条的6种方式

普通进度条

for i in range(1, 101):
    print("\r", end="")
    print("进度: {}%: ".format(i), "▓" * (i // 2), end="")
    sys.stdout.flush()
    time.sleep(0.05)

带时间的普通进度条

t = 60
print("**************带时间的进度条**************")
start = time.perf_counter()
for i in range(t + 1):
    finsh = "▓" * i
    need_do = "-" * (t - i)
    progress = (i / t) * 100
    dur = time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(progress, finsh, need_do, dur), end="")
    time.sleep(0.05)

tqdm库

tqdm是专门用于进度条的一个python库,详细的用法可以参考下方官方文档

https://pypi.org/project/tqdm/#description

from tqdm import tqdm
for i in tqdm(range(1, 60)):
    """
    代码
    """
    # 假设这代码部分需要0.05s,循环执行60次
    time.sleep(0.05)

alive_progress库

alive_progress是一个动态的实时显示进度条库,详细的用法可以参考下方官方文档

https://pypi.org/project/alive-progress/#description

from alive_progress import alive_bar
# 假设需要执行100个任务
with alive_bar(len(range(100))) as bar:
    for item in range(100): # 遍历任务
        bar()  # 显示进度
        """
        代码
        """
        # 假设这代码部分需要0.05s
        time.sleep(0.05)

PySimpleGUI库

PySimpleGUI也是一种动态进度条库,该库是自带GUI界面(基于PyQt,Tkinter等),详细的用法可以参考下方官方文档

https://pypi.org/project/PySimpleGUI/#description

import` `PySimpleGUI as sg
count ``=` `range``(``100``)
for` `i, item ``in` `enumerate``(count):
  ``sg.one_line_progress_meter(``'实时进度条'``, i ``+` `1``, ``len``(count), ``'-key-'``)
  ``"""
  ``代码
  ``"""
  ``# 假设这代码部分需要0.05s
  ``time.sleep(``0.05``)

progressbar库

https://pypi.org/project/progressbar/#description

import progressbar
p = progressbar.ProgressBar()
# # 假设需要执行100个任务,放到ProgressBar()中
for i in p(range(100)):
    """
    代码
    """
    # 假设这代码部分需要0.05s
    time.sleep(0.05)

posted @   邵泽龙  阅读(2387)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示