我想让 *
以 1 秒的间隔在命令行上闪烁。
import time
from sys import stdout
while True:
stdout.write(' *')
time.sleep(.5)
stdout.write('\r ')
time.sleep(.5)
我得到的只是一个空行,没有闪烁的 *
。
这是为什么?
最佳答案
检查一下。这将以 0.5 秒的间隔在一行上打印 *
,并显示 0.5 秒(如您所说的那样闪烁)
import time
while True:
print('*', flush=True, end='\r')
time.sleep(0.5)
print(' ', flush=True, end='\r')
time.sleep(0.5)
请注意,这在 IDLE 中不起作用,但使用 cmd 时它可以正常工作。
不使用两个打印语句,你可以这样做:
import time
i = '*'
while True:
print('{}\r'.format(i), end='')
i = ' ' if i=='*' else '*'
time.sleep(0.5)
关于python - 在 python shell 上制作一个闪烁的忙碌指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36674866/