7

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

6 Answers6

17

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.

wjandrea
  • 16,334
  • 5
  • 30
  • 53
Juan T
  • 1,163
  • 1
  • 8
  • 20
  • 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? –  Feb 26 '17 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 '17 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? –  Feb 26 '17 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 '17 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) –  Mar 13 '17 at 12:33
  • If you are getting a number at the end of your output then assign it to a variable e.g : `a = color.write("Yes","STRING")`. – Black Thunder Aug 03 '18 at 11:54
  • Thank you @JuanT for this solution! Learning Python with the kids and expanded on your explanation as colorama and ANSI codes not working in Python 3.8.2 IDLE terminal. To help out others: **Script 1** `import sys try: color = sys.stdout.shell except AttributeError: raise RuntimeError("Use IDLE") White="SYNC" White2="stdin" Purple="BUILTIN" Green="STRING" Orange="console" Red="COMMENT" Aqua="stdout" White3="TODO" Pink="stderr" HighlightWhite="hit" Blue="DEFINITION" Rose="KEYWORD" HighlightPink="ERROR" HighlightGrey="sel" ` – Chris Charles Apr 15 '20 at 12:42
  • **Script_2** `from Script_1 import * color.write(f"Your random text goes here:\n",HighlightGrey)` – Chris Charles Apr 15 '20 at 12:49
  • @BlackThunder if you aren't planning on using the variable it's better to use `_`. – Seth Feb 22 '21 at 16:55
3

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()
wjandrea
  • 16,334
  • 5
  • 30
  • 53
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?

ack
  • 1,451
  • 1
  • 14
  • 18
0

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

Coder
  • 1
-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)
yawn
  • 1
  • 1
  • 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 '18 at 07: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 '18 at 09:00
-2

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?

Dartmouth
  • 1,016
  • 2
  • 16
  • 22