等待动画“……"一遍遍地出现 [英] Wait animation "..." to appear over and over

查看:23
本文介绍了等待动画“……"一遍遍地出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示一种加载动画,特别是反复出现的三个点.我认为以下方法可行:

I'm trying to display a sort of loading animation, specifically three dots appearing over and over. I thought the below would work:

import time, sys

for i in range(9):
    time.sleep(0.2)
    x = i % 4
    sys.stdout.write("." * x)
    sys.stdout.flush()

然而,它所做的只是逐渐(并且不均匀地)加载:"........"

However all it does is just gradually (and non-uniformly) load this: "........."

推荐答案

使用 \r 返回字符.这是因为你的光标在经过一段时间后应该回到原来的位置

Use \r the return character. This is because your cursor should return to the original position after you lapse the time

import time, sys

for i in range(15):
    sys.stdout.write("   ")
    x = i % 4
    sys.stdout.write('\r' + "." * x )
    time.sleep(0.5)
    sys.stdout.flush()

这篇关于等待动画“……"一遍遍地出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆