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

后端 未结 6 1615
谎友^
谎友^ 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:14

    Put this at the "start" of your code:

    import sys
    
    try: color = sys.stdout.shell
    except AttributeError: raise RuntimeError("Use IDLE")
    

    And then use color.write(YourText,Color) for "printing":

    color.write("Hi, are you called Miharu461? \n","KEYWORD")
    color.write("Yes","STRING")
    color.write(" or ","KEYWORD")
    color.write("No\n","COMMENT")
    

    This prints:


    Note: this does NOT automatically puts the enter (like in the print function/statement). So, when you are printing put \n at the end of the last string to put it.

    The "Colors" you can put are: SYNC, stdin, BUILTIN, STRING, console, COMMENT, stdout, TODO, stderr, hit, DEFINITION, KEYWORD, ERROR, and sel.

    Note 2: This is dependent of the color scheme you are using for IDLE. So I recommend you to use it for highlighting, and not for making a program for asking what in color is some word.

提交回复
热议问题