문제

So I made a makeshift progress bar. Here's a mock version:

import sys, time

def Round(n):
    return str("{0:.2f}".format(n))

def updateProgressBar(label, percentage):
    width = 20
    done = width * int(percentage)/100
    sys.stdout.write('\r' + label + '\t[' + done * '*' + (width-done) * ' ' + ']\t' + Round(percentage) + '%')

n = 23
for i in range(n+1):
    time.sleep(0.25)
    updateProgressBar('Testing', 100*float(i)/n)

It's working just fine when ran from cmd, growing the % and incrementing the bar length. But when I run it through cygwin, nothing is displayed till the bar is at 100%, at which point it appears entirely, which defies the point. Any pointers please?

도움이 되었습니까?

해결책

Figured it out. Adding sys.stdout.flush() did the trick. I don't fully understand why though, and would be curious to hear if someone can explain.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top