我有三个进度条,我需要在每行中显示进度条

时间:2019-08-21 07:18:51

标签: python progress-bar

我的代码中有三个进度条(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)

1 个答案:

答案 0 :(得分:0)

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

看看这个this答案以及该页面上的后续答案以了解更多信息。

如果您使用的'\ r'和std.flushes过多,则会导致输出奇怪的结果:D