Print in colour in IDLE.
These functions will ONLY work in IDLE, they use a side effect of the IDLE syntax highlighting. Only use idlecolors for fun. To use colours properly in a terminal please use colorama or crayons
Right click the link to download the file idlecolors.py and save it onto your computer.
If you are using a Raspberry Pi, you could open a terminal and type the following command to copy the file:
git clone https://github.com/lawsie/idlecolors.git
Open IDLE. Create a Python program in the same folder as the idlecolors.py
file and add the following statement once at the start of your program:
from idlecolors import *
Then, you can use the printc()
function along with the colour functions:
printc( red("Red text") )
printc( "If you add " + red("red") + " to " + blue("blue") + ", you get " + purple("purple") )
You can also randomly select colours like this:
# Print a line in a random colour
printc( randcol("This is a random colour") )
By default, the printc()
function adds a newline character at the end of its output, just like print()
. You can change the end character in
the same way as you would with the print function by specifying an end
argument as in the example below.
# Print each word in a random colour
mytext = "This is a random piece of text which I want to print in random colours"
mytext = mytext.split(" ")
for word in mytext:
printc(randcol(word), end=" ")
You can use the colours red()
, orange()
, green()
, blue()
, purple()
, black()
, brown()
and randcol()
for a random colour from this selection.
These colours are defined by the default syntax highlighting in IDLE (Options > Configure IDLE). If you have changed IDLE's colour scheme, the colours will not match!
Yes, but it is not particularly straightforward and involves some configuration. You can define 5 custom user colours manually:
- In IDLE, select
Options
>Configure IDLE
, then click on theHighlights
tab. - Click the drop down
Normal text
and select a type of text to change. In this example we will choose "Python Definitions" which is known inidlecolors
as the colouruser1()
.
- Select a new colour by clicking on the
Choose colour for
drop down and selecting a colour from the palette:
- Now open your copy of
idlecolors.py
file and change theUSE_CUSTOM_COLORS
value toTrue
. (Be aware that this will also allow all of the user defined colours to be randomly chosen.)
USE_CUSTOM_COLORS = True # Change to True if you want to use custom colours
- You can now use the colour you selected for "Python Definitions" as
user1()
.
from idlecolors import *
printc( user1("New colour"))
If you wish, you can change the name of the colour by editing the idlecolors.py
file - you will need to edit both the colormap
key and the function name.
Yes, yes it is.
Credit for this idea came from this Stackoverflow thread. I made this because kids like colours. It's not at all meant to be used for anything serious.