How to print colored text in Python?

前端 未结 30 2977
谎友^
谎友^ 2020-11-21 04:41

How can I output colored text to the terminal in Python?

30条回答
  •  自闭症患者
    2020-11-21 05:12

    asciimatics provides a portable support for building text UI and animations:

    #!/usr/bin/env python
    from asciimatics.effects import RandomNoise  # $ pip install asciimatics
    from asciimatics.renderers import SpeechBubble, Rainbow
    from asciimatics.scene import Scene
    from asciimatics.screen import Screen
    from asciimatics.exceptions import ResizeScreenError
    
    
    def demo(screen):
        render = Rainbow(screen, SpeechBubble('Rainbow'))
        effects = [RandomNoise(screen, signal=render)]
        screen.play([Scene(effects, -1)], stop_on_resize=True)
    
    while True:
        try:
            Screen.wrapper(demo)
            break
        except ResizeScreenError:
            pass
    

    Asciicast:

提交回复
热议问题