命令提示符中的'等待'动画(Python)

Sar*_*rah 12 python animation command

我有一个Python脚本需要很长时间才能运行.我非常希望让命令行输出有一些"等待"动画,就像我们在AJAX请求的浏览器中得到的旋转圆圈一样.类似于'\'的输出,然后将其替换为'|',然后是'/',然后是' - ','|'等,就像文本在圆圈中一样.我不知道如何在Python中替换以前的打印文本.

AKX*_*AKX 22

使用\r和print-without-newline(即带逗号的后缀):

animation = "|/-\\"
idx = 0
while thing_not_complete():
    print animation[idx % len(animation)] + "\r",
    idx += 1
    time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)

对于Python 3,请使用以下print语法:

print(animation[idx % len(animation)], end="\r")
Run Code Online (Sandbox Code Playgroud)


LPb*_*Pby 9

只是另一个漂亮的变种

import time

bar = [
    " [=     ]",
    " [ =    ]",
    " [  =   ]",
    " [   =  ]",
    " [    = ]",
    " [     =]",
    " [    = ]",
    " [   =  ]",
    " [  =   ]",
    " [ =    ]",
]
i = 0

while True:
    print(bar[i % len(bar)], end="\r")
    time.sleep(.2)
    i += 1
Run Code Online (Sandbox Code Playgroud)


War*_*ren 6

如果您正在安装某些东西,加载栏很有用。

animation = [
"[        ]",
"[=       ]",
"[===     ]",
"[====    ]",
"[=====   ]",
"[======  ]",
"[======= ]",
"[========]",
"[ =======]",
"[  ======]",
"[   =====]",
"[    ====]",
"[     ===]",
"[      ==]",
"[       =]",
"[        ]",
"[        ]"
]

notcomplete = True

i = 0

while notcomplete:
    print(animation[i % len(animation)], end='\r')
    time.sleep(.1)
    i += 1
Run Code Online (Sandbox Code Playgroud)

如果你想让它持续几秒钟

if i == 17*10:
    break
Run Code Online (Sandbox Code Playgroud)

之后

i += 1
Run Code Online (Sandbox Code Playgroud)