zoukankan      html  css  js  c++  java
  • Python之控制台进度条实现

    进度条一

    源码实现

    import sys
    import time
    
    
    def a1():
        for i in range(1, 101):
            print('
    ', end="")
            print("进度: {}%: ".format(i), "||" * (i // 2), end="")
            sys.stdout.flush()
            time.sleep(0.05)
    
    if __name__ == '__main__':
        a1()
    

    效果图

    image

    进度条二

    源码实现

    import time
    
    
    def a2():
        scale = 50
        start = time.perf_counter()
        for i in range(scale + 1):
            a = "*" * i
            b = "." * (scale - i)
            c = (i / scale) * 100
            dur = time.perf_counter() - start
            print("
    {:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
            time.sleep(0.1)
    
    
    if __name__ == '__main__':
        a2()
    

    效果图

    image

    image

    进度条三

    安装模块

    pip install tqdm
    

    源码实现

    import time
    from tqdm import tqdm
    
    
    def a3():
        time.sleep(5)
        for i in tqdm(range(1, 101)):
            # io的操作
            time.sleep(0.1)
        time.sleep(1)
    
    
    if __name__ == '__main__':
        a3()
    

    效果图

    image

    进度条四

    安装模块

    pip install progress
    

    源码实现

    import time
    from progress.bar import Bar
    
    
    def a4():
        b = Bar("下载", max=100)
        for item in range(100):
            b.next()
            time.sleep(1)
            b.finish()
    
    
    if __name__ == '__main__':
        a4()
    

    注意事项:

    • PyCharm中的控制台是看不到效果的。只有在cmd才可以。

    效果图

    image

    进度条五

    安装模块

    pip install alive_progress
    

    源码实现

    import time
    from alive_progress import alive_bar
    
    
    def a5():
        time.sleep(5)
        a = range(100)
        with alive_bar(len(a)) as bar:
            for _ in a:
                bar()
                time.sleep(0.3)
    
    
    if __name__ == '__main__':
        a5()
    

    效果图

    image

    可视化进度条

    安装模块

    pip install PySimpleGUI
    

    源码实现

    import time
    import PySimpleGUI as ui
    
    def a6():
        a = [i for i in range(1, 9)]
        for i, item in enumerate(a):
            ui.one_line_progress_meter("下载", i + 1, len(a), '下载')
            time.sleep(1)
    
    
    if __name__ == '__main__':
        a6()
    

    效果图

    image

  • 相关阅读:
    Web 前端 UI 组件库文档自动化方案 All In One
    how to auto open demo and create it in a new codesandbox
    TypeScript & Canvas 实现可视化白板
    2020~2021 职业规划书
    PostCSS All In One
    zsh terminal set infinity scroll height
    npm version ^ meaning
    vue-cli & webpack & vue.config.js
    [HDOJ5350]MZL's munhaff function
    [POJ3061]Subsequence
  • 原文地址:https://www.cnblogs.com/zhenzi0322/p/15450698.html
Copyright © 2011-2022 走看看