【问题标题】:How to print blinking text that blinks between two different text while blinking python如何在闪烁python时打印在两个不同文本之间闪烁的闪烁文本
【发布时间】:2021-07-07 15:07:50
【问题描述】:

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

我正在使用 imageDraw 为文本绘制输出,而 termcolor 库不适用于 python 3.8。请让我知道是否有其他方法可以做到这一点。

print("Hello World")

print("你好吗?")

【问题讨论】:

    标签: python


    【解决方案1】:

    使用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)
    

    【讨论】:

    • 在输出端闪烁时不清除整行
    • 原来它在 Windows 上的 IDLE 上无法正常工作,但在通过终端 (linux) 运行时可以工作
    猜你喜欢
    • 2017-03-08
    • 2018-11-01
    • 2018-01-30
    • 1970-01-01
    • 2020-02-29
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多