8

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 documentation).

I just want to print text in a color other than blue when I'm outputting in IDLE. Is it possible? What's an easy way to do this? I'm running Python 3.6 on Windows.

Please explain with an example.

(I have found that ANSI codes do not work inside IDLE, only on the terminal.)

5 Answers 5

19

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:

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.

9
  • This also prints the number of characters printed with color.write immediately after the printed string. Is there a way to only print the string?
    – user4728253
    Feb 26, 2017 at 19:40
  • 1
    Use \n as an Enter key. Example: color.write("Foo\n","STRING") works like print. Also, color.write is the name I gave it because I put color = sys.stdout.shell. You can use anything instead of "color".
    – Juan T
    Feb 26, 2017 at 20:47
  • That wasn't my question. There's extra unexpected output using the write method: it prints the number of characters in the string immediately after the string. How do I get rid of it?
    – user4728253
    Feb 26, 2017 at 22:45
  • At least on my computer, it doesn't print that. I added a picture of the output I get in the answer.
    – Juan T
    Feb 26, 2017 at 23:09
  • What I see is a blue number directly after the output that's the total number of characters printed. So like, there might be a 38 printed after "No" if I tried running this. (I just counted...I don't know if it's exactly 38)
    – user4728253
    Mar 13, 2017 at 12:33
4

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

Install:

pip install clrprint

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()
1
  • 1
    It would have been better if you didn't use the from [...] import * syntax, because i make complicated to know what come from the module.
    – Camion
    Jul 3, 2021 at 0:37
2

According to Why does writing to stdout in console append the number of characters written, in Python 3? "... 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

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

to "eat up" the extra output. _ is a "throwaway" variable, see What is the purpose of the single underscore “_” variable in Python?

2
  • 1
    if you aren't planning on using the variable it's better to use _.
    – Seth
    Feb 22, 2021 at 16:55
  • @Seth Thanks for the hint - I edited my answer accordingly.
    – AcK
    Feb 22, 2021 at 17:20
0

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

-1

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)
2
  • Hi @yawn, it looks like this does not answer the question. Please keep in mind this is not a forum, so only post an answer if you have something that can actually solve the OP question.
    – toti08
    Sep 13, 2018 at 7:18
  • Sorry, @toti08, I think I just follow the ruler "Provide details and share your research!" and my method does achieve printing colored text other then blue in IDLE's terminal. Please let me know how to adjust my answer, thank you very much.
    – yawn
    Sep 14, 2018 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.