pythonpython-idlecolorama

Python - Changing IDLE Text color not working on Windows


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


Solution

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