简介(骑在星际办公椅上)
我有一个程序,它使用进度条来显示最终用户程序是否工作。这应该告诉最终用户他们需要等待多长时间以及程序是否仍在工作。这个问题是在阅读这个有趣的堆栈溢出线程后提出的:Text progress bar in terminal with block characters
问题/挑战
第一个问题是进度条目前只在一个循环中工作,只打印出范围内的数字。这是没有帮助的。为什么有人想要一个进度条只显示单个循环的进度,而不是程序的其余部分正在做什么的程序。
第二个问题是,当我有其他打印语句与进度条有突然多个进度条打印到命令提示符,而不是一个单一的进度条,似乎动画与每个进度条更新。
我怎样才能让进度条总是在终端的底部,而其他的打印语句在它上面呢?
另一个挑战是我正在使用Windows 7操作系统(OS)。
编码
请查看我为您提供的以下示例代码:
import sys
import time
import threading
def progress_bar(progress):
sys.stdout.write('\r[{0}] {1}'.format('#' * int(progress/10 * 10), progress))
def doThis():
for i in range(10):
print("Doing this.")
def doThat():
for i in range(3):
print("Doing that.")
def wrapUp():
total = 2+ 2
print("Total found")
return total
if __name__ == "__main__":
print("Starting in main...")
progress_bar(1)
print("\nOther print statement here.")
print("Nice day, expensive day.")
progress_bar(3)
doThis()
progress_bar(4)
doThat()
progress_bar(5)
doThis()
doThat()
progress_bar(6)
progress_bar(7)
doThat()
doThat()
progress_bar(8)
wrapUp()
progress_bar(9)
progress_bar(10)
程序打印的内容
Starting in main...
[#] 1
Other print statement here.
Nice day, expensive day.
[###] 3Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
[####] 4Doing that.
Doing that.
Doing that.
[#####] 5Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing that.
Doing that.
Doing that.
[#######] 7Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
[########] 8Total found
[##########] 10
1条答案
按热度按时间zzwlnbp81#
你必须做三件事:
你可以稍微涵盖前两个,但不包括第三个。最好将控制台的控制封装在一个类中,这个类也可以管理进度条,这样它就可以在一个位置处理清除、打印和重新显示:
我在示例代码导入
threading
时包含了一个线程锁,因此我假设您希望能够在这样的环境中使用它。上面的类使用固定宽度的进度条,在返回到最左边的列
\r
之前,通过写出一系列空格来清除进度条。我还将条形图设置为固定宽度,以便它从左到右填充,而不是在屏幕上扩展。
然后确保你'打印'到这个对象:
上面的最终输出是:
插入一些随机睡眠,运行时看起来像这样: