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