【问题标题】:I have three progress bars and I need to display progress bars in each line我有三个进度条,我需要在每一行显示进度条
【发布时间】:2019-08-21 07:18:51
【问题描述】:

我的代码中有三个进度条(1 个主进度条和 2 个子进度条)。我需要在新行中一一显示这些进度条。

我已经尝试在函数中包含新行,但那会为每次迭代创建一个新行,看起来不太好。


        #Progress Bar
    def progress(count, total, status=''):
            bar_len = 40
            filled_len = int(round(bar_len * count / float(total)))

            percents = round(100.0 * count / float(total), 1)
            bar = '=' * filled_len + '-' * (bar_len - filled_len)

            sys.stdout.flush()
            sys.stdout.write('\r[%s] %s%s - %s\r' % (bar, percents, '%', status))

            sys.stdout.flush()
            sys.stdout.write("\r")

    def sub_progress_source(count, total, status=''):
            bar_len = 40
            filled_len = int(round(bar_len * count / float(total)))

            percents = round(100.0 * count / float(total), 1)
            bar = '=' * filled_len + '-' * (bar_len - filled_len)

            sys.stdout.flush()
            sys.stdout.write('\r\n\t[%s] %s%s - %s\r' % (bar, percents, '%', status))

            sys.stdout.flush()
            sys.stdout.write("\r")

    def sub_progress_target(count, total, status=''):
            bar_len = 40
            filled_len = int(round(bar_len * count / float(total)))

            percents = round(100.0 * count / float(total), 1)
            bar = '=' * filled_len + '-' * (bar_len - filled_len)
            sys.stdout.flush()
            sys.stdout.write('\r\n\t[%s] %s%s - %s\r' % (bar, percents, '%', status))    
            sys.stdout.flush()
            sys.stdout.write("\r")
[====------](Main Progress Bar)
    [====-----](Sub Progress Bar 1)
    [=======--](Sub Progress Bar 2)

【问题讨论】:

    标签: python progress-bar


    【解决方案1】:

    要覆盖控制台中的多行,您需要诸如 curses(基于 unix 的系统)或 win32 控制台 api(windows)之类的库

    查看此this 答案并关注该页面上的答案以了解更多信息。

    顺便说一句,你使用了太多的 '\r' 和 std.flushes,这会导致你得到奇怪的输出:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 2014-05-25
      • 2023-03-10
      • 2013-02-22
      相关资源
      最近更新 更多