Python – Changing IDLE Text color not working on Windows

Question:

I am using colorama to try to simulate in the IDLE shell. Here is my code:

from colorama import Fore, Back, Style

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

My output looks something like this:

Error

What is incorrect? Why does it print those wierd letters in the start? I am using Windows OS.

P.S: I’ve also tried running this in Command Prompt, and I got a similar output

Asked By: xilpex

||

Answers:

You’re missing a call to init (scroll down to “Usage”):

from colorama import Fore, Back, Style, init

# Here
init()

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

Which outputs, colored

some red text
and with a green background
and in dim text

back to normal now

This still doesn’t work in IDLE, but works in cmd and powershell.

Answered By: Carcigenicate

On Windows, Colorama assumes that output goes to the Windows text console. The Command Prompt uses that console. So does python.exe when started from an icon or Start menu entry. Colorama sends ANSI escape codes and also makes win32 calls understood by the console. It does not work directly work with graphics frameworks whose text widgets have text colored by an different method.

Answered By: Terry Jan Reedy

if you’re using python idle colorama just does not work, other ide’s like replit, vscode etc… can use colorama with no problem, and according to ‘Carcigenicate’ using the init() allows cmd and powershell to use colorama correctly.

Answered By: moses joeseph
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.