python - 命令提示符中的“等待”动画 (Python)

标签 python animation command

我有一个需要很长时间才能运行的 Python 脚本。我非常希望命令行输出有一点“等待”动画,就像我们在浏览器中为 AJAX 请求获得的漩涡状圆圈一样。类似于 '\' 的输出,然后将其替换为 '|',然后是 '/',然后是 '-'、'|' 等,就像文本在循环中一样。我不确定如何用 Python 替换以前打印的文本。

最佳答案

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

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

对于 Python 2,使用此 print 语法:

print animation[idx % len(animation)] + "\r",

关于python - 命令提示符中的“等待”动画 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7039114/

相关文章:

Python运行时错误: maximum recursion depth exceeded in cmp

CSS3动画-变换: scaleX(-1) - go/back - size down - why?

css - 为什么 SVG 在 webkit 浏览器下缩放时会变得模糊?

.net - 使用 MultiBinding 时将枚举值作为命令参数传递

python - PyPandoc 与 PyInstaller 结合使用

python - 在 NDB 中跟踪未知类型的反向引用

python - 如何从pandas中的csv文件中进行计数和百分比

html - 悬停时将背景图像更改为颜色

django - 使用参数和选项编写自定义管理命令 - 所需字段的解释

unix - 如何让unix脚本每15秒运行一次?