如何在闪烁python时打印在两个不同文本之间闪烁的闪烁文本

时间:2021-07-07 15:07:50

标签: python

我必须打印语句,并且我希望打印语句在两个语句之间闪烁。我想弄清楚如何打印 hello world 并让它闪烁到你好吗?

我正在使用 imageDraw 为文本绘制输出,并且 termcolor 库不适用于 python 3.8。如果有其他方法可以做到这一点,请告诉我。

<块引用>

print("Hello World")

<块引用>

print("你好吗?")

1 个答案:

答案 0 :(得分:0)

通过使用 ANSI escape sequences 有一种无需任何库的本机方法即可做到这一点。

您还可以组合使用回车来“替换”打印的文本。

import sys
from time import sleep

print('Hello World', end='\r')
sleep(1)

for _ in range(5):  # Change to control no. of 'blinks'
    print('How are you?', end='\r')
    sleep(1)  # To create the blinking effect
    sys.stdout.write('\033[2K\r')  # Clears the entire line and carriage returns
    sleep(1)