How do I print colored text in IDLE's terminal?

后端 未结 6 1638
谎友^
谎友^ 2020-12-11 19:50

This is very easily a duplicate question--because it is. However, there are very many inadequate answers to this (e.g. try curses! -- pointing to a 26 page docu

6条回答
  •  无人及你
    2020-12-11 20:21

    The problem I faced was how to output error messages in IDLE properly, since sys.exc_info() is printed like normal output while traceback.print_exc() is a bit of too long and detailed. So, I just want to print the Exception in red color like "traceback".

    Thanks the top anwser for letting me learn about specifying the type of my messages with write(). Accidentally, I've found another method that meets my demmand by simplely adding file=sys.stderr while print(). Then I'll get a striking but brief error messages. An example in python 3.6:

    import sys
    try:
        1/0
    except Exception as e:
        print(repr(e), file=sys.stderr)
    

提交回复
热议问题