0

I wanted to change the colour of text I output. for example:

print("This text should be in Blue")
print("This text should be in red")

I have read around and tried various methods without success. What is the simplest way to change the colour in IDLE.

Thanks

I have been through the suggestions in the other posts but i get the following printed to the screen all in blue font:

[91mprinting red colour[91m

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Moridiin
  • 11
  • 3
  • 7
  • This is a duplicate of http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python. – cfh Mar 28 '15 at 10:07
  • i have been through the suggestions in the other posts but i get the following printed to the screen all in blue font: [91mprinting red colour[91m – Moridiin Mar 28 '15 at 10:26
  • @cfh: the question is not a duplicate. IDLE doesn't support ANSI color codes. – jfs Mar 28 '15 at 16:28
  • @J.F.Sebastian: IDLE as a requirement was edited in later. – cfh Mar 28 '15 at 18:05
  • @cfh: I see. The question *was* a duplicate until [the 3rd revision](http://stackoverflow.com/revisions/29315467/3) – jfs Mar 28 '15 at 18:56
  • Possible duplicate of [How to print colored text in terminal in Python?](https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python) – Nazim Kerimbekov May 01 '19 at 07:15

1 Answers1

1

The answer is that you cannot do what you want with Idle as it is.

When running Python code with Idle, programmatically changing the display color of stdout text output is not possible for the following reasons. The Idle Shell and Editor windows contain tkinter Text widgets that serve as proxies for text widgets in the tcl/tk gui framework. Statements at a Shell prompt or code in an edit window are run in a separate process. Idle captures strings sent to stdout and stderr, inserts the strings into the text widget, and tells tk to color the text blue or red (or whatever you choose in Options -> Configure Idle -> Highlighting).

Importing textcolor, colorama, or any other module in your code can change the strings sent to stdout. But they do not change how Idle code handles the string when displaying it in a Text widget. Neither Idle nor tkinter nor tk interpret the text. On Windows, print('\033') andprint('\x1b')` display a left arrow. (When copied to an SO Answer edit box, ESC shows as a square with 0, 0, 1, B. When the answer is displayed, ESC is ignored and nothing shows.)

I have in mind adding an option to Idle to run editor code in a console window, with Idle not involved. Another idea is a Text subclass that would interpret ANSI codes, including color codes, before inserting text. The first will be pretty easy; the second will take more work; both would be useful in different situations, and both are in the future.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • `colorama` can replace ANSI escape codes with corresponding Win32 API calls to color the output text in Windows console. – jfs Mar 28 '15 at 23:23
  • I am aware that this is how colorama makes textcolor work in a Windows console that does not recognize ansi code, but did not want to complicate the answer. Whether or not colorama makes Win32 api calls in the windowless user process can have no effect on the Text widget in a separate process. I am sure that the same is true even if running Idle with the -n option. – Terry Jan Reedy Mar 29 '15 at 22:14
  • your answer might have implied that ANSI codes won't work on Windows. I've pointed out how `colorama` can make them work in the console. It changes nothing inside IDLE. – jfs Mar 29 '15 at 22:16