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

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

    The short answer: No, it isn't possible to colour different outputs in the IDLE shell.

    The only colour changes you can make it to change the entire colour theme, but this will affect all printed strings. If you need coloured output, you should probably switch away from a basic IDE like IDLE or at least a more complete shell than the basic IDLE shell. See this duplicate: Change printed text color in Idle?

    0 讨论(0)
  • 2020-12-11 20:10

    You can use clrprint module to print color text in idle, Terminal and Powershell too.

    Install:

    pip install clrpirnt

    Usage:

    from clrprint import *
    clrhelp() # to see colors available
    user_input = clrinput('INPUT MESSAGE',clr='green') # just like input()
    clrprint('YOURTEXT',user_input,clr='color')   # just like print()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • According to Strange output when writing to stdout in console "... write will also return the number of characters (actually, bytes, try sys.stdout.write('へllö')) As the python console prints the return value of each expression to stdout, the return value is appended to the actual printed value."

    Use

    dummy = color.write("Hello world","COMMENT")
    

    to "eat up" the extra output.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-11 20:25

    The strnage output of the length is with the return keyword, and NORMAL is also a color

    0 讨论(0)
提交回复
热议问题