我想在模块中添加动画,并且要动态控制多行,但是我找不到合适的选项。
确切信息:
D:\Project> python download.py Connection Established :) Downloading 50/500 files... . __ |\ __/__\___________| \_ | ___ ___`-. | / \ 10% / \ `-. |_| (O) |________| (O) |____| \___/ \___/
50%之后
D:\Project> python download.py Connection Established :) Downloading 250/500 files... . __ |\ __/__\___________| \_ | ___ ___`-. | / \ 50% / \ `-. |_| (O) |________| (O) |____| ............................... \___/ \___/
答案 0 :(得分:0)
您可以使用以下方法在特定屏幕位置打印字符串:
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (row, col, text))
以下代码将完成这项工作:
import os, sys, time
def update_screen(done, total):
percent = int((done/total)*100)
x_location = (percent//2)+1
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (2, 0, "Connection Established :)"))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (3, 0, "Downloading {0}/{1} files...".format(done, total)))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (4, x_location, " . "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (5, x_location, " __ |\ "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (6, x_location, " __/__\___________| \_ "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (7, x_location, " | ___ ___`-. "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (8, x_location, " | / \ / \ `-. "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (9, x_location, " |_| (O) |________| (O) |____|"))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (10,x_location, ". \___/ \___/ "))
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (8, x_location+12, "{: 2d}%".format(percent)))
sys.stdout.flush()
def main():
os.system("clear")
for i in range(500):
update_screen(i, 500)
time.sleep(0.2)
if __name__ == "__main__":
main()