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

5

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

Nerissa answered 26/2, 2017 at 19:14 Comment(0)
C
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.

Cheddar answered 26/2, 2017 at 19:31 Comment(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?Nerissa
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".Cheddar
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?Nerissa
At least on my computer, it doesn't print that. I added a picture of the output I get in the answer.Cheddar
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)Nerissa
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").Ventriculus
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" Catiline
Script_2 from Script_1 import * color.write(f"Your random text goes here:\n",HighlightGrey)Catiline
@BlackThunder if you aren't planning on using the variable it's better to use _.Nievelt
I
6

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()
Induration answered 16/7, 2020 at 8:49 Comment(2)
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.Antetype
While this might work, it heavily depends on a particular hilight color setting being available in the IDLE configuration.Cressler
G
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?

Gastrostomy answered 31/7, 2017 at 8:28 Comment(2)
if you aren't planning on using the variable it's better to use _.Nievelt
@Nievelt Thanks for the hint - I edited my answer accordingly.Gastrostomy
M
0

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

Martinet answered 27/4, 2020 at 13:54 Comment(0)
M
-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)
Mantis answered 13/9, 2018 at 7:12 Comment(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.Vanden
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.Mantis

© 2022 - 2024 — McMap. All rights reserved.