Terminal Colour in Python
GUIs?
No, not in this one. However, we will be covering GUIs in the next set of tutorials. In this one, we will be covering the Command Line Interface, or CLI for short. We have the ability, with Linux based Systems, to use a thing called ANSI. This allows us to create coloured text in the terminal, which is pretty cool for little programs or terminal based games.
Colorama
We will be using the colorama
module to create the coloured text. This is because it also supports the Windows CLI as well, and the fact that it is super easy to use. So make sure that you install it using pip or Repl's Package Manager. Now we can import it like so:
# import colorama but as cr, as cr is easier to use.
import colorama as cr
One quick thing I'd like to mention about colorama
is that you can initialise it. We will be using this method:
# Initialise colorama and make it so that the colours auto reset
cr.init(autoreset=True)
So that each print
statement has its colours reset. If you want the colours to be kept, then you can remove the autoreset=True
or set it to False
, which it is by default.
Let's begin!
Let's Do This!
So, we are going to be using the good old Hello, World!
to look at how colorama
works. So let's begin by making the Hello,
look red, and the World!
look blue. We will then print Hello, World!
after to show that the colours don't continue:
# Print with foreground colors
print(f"{cr.Fore.RED}Hello, {cr.Fore.BLUE}World!")
# Print without colors
print("Hello, World!")
As you can see, we used cr.Fore
, this means that we are changing the Foreground colours. You can also change the Background colours of text like so:
# Print with background colors
print(f"{cr.Back.RED}Hello, {cr.Back.BLUE}World!")
# Print without colors
print("Hello, World!")
Pretty cool, but are there any other colours? Yes there are! These are the colours that come with colorama
# All colors
print(f"{cr.Fore.BLACK}BLACK")
print(f"{cr.Fore.BLUE}BLUE")
print(f"{cr.Fore.CYAN}CYAN")
print(f"{cr.Fore.GREEN}GREEN")
print(f"{cr.Fore.MAGENTA}MAGENTA")
print(f"{cr.Fore.RED}RED")
print(f"{cr.Fore.WHITE}WHITE")
print(f"{cr.Fore.YELLOW}YELLOW")
print()
print(f"{cr.Back.BLACK}BLACK")
print(f"{cr.Back.BLUE}BLUE")
print(f"{cr.Back.CYAN}CYAN")
print(f"{cr.Back.GREEN}GREEN")
print(f"{cr.Back.MAGENTA}MAGENTA")
print(f"{cr.Back.RED}RED")
print(f"{cr.Back.WHITE}WHITE")
print(f"{cr.Back.YELLOW}YELLOW")
print()
# If you don't use auto reset, you can reset your colors like so
print(f"{cr.Fore.RESET}RESET")
print(f"{cr.Back.RESET}RESET")
You'll also notice that you can reset your colours. This is for when you don't use autoreset
. It allows you to revert the colour back to defaults.
You can also add brightness to your colours. This helps if you need to use a colour more than once, or if you need to be, well, more or less bright:
# Adding opacity to colors
print(f"{cr.Fore.RED}Hello, {cr.Style.DIM}World!")
print(f"{cr.Fore.RED}Hello, {cr.Style.NORMAL}World!")
print(f"{cr.Fore.RED}Hello, {cr.Style.BRIGHT}World!")
We use Style
to do this. You'll see a gradient in the brightness from DIM
to NORMAL
to BRIGHT
. We can also reset all our colours and brightness using Style
like so:
# Resetting all colors, without autoreset
print(f"{cr.Back.WHITE}{cr.Fore.BLACK}Hello, {cr.Style.RESET_ALL}World!")
Which is very useful to know if you have set all three and don't want to have to reset each one individually.
Conclusion
Being able to change the colour of text in the terminal is very useful. It can be used for a variety of things, making dull text look much more appealing. I hope this helps you in you adventures!
Have a great day!
P.S
This tutorial is one in a series of tutorials suggested by @enigma_dev .
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!
How do I do to make this work after following the instructions above?? I put it in terminal or do I put it in a file, save and execute??
For the terminal:
- Input each line individually.
For a file:
- Place all the code in a file
- Execute said file
The choice is up to you on how you want to use it.
Thank you @LeonDoesCode
@IsaSofia Glad I could help!
There is a much simpler way of using Colorama that makes it easier to use and read (at least to me), as follows:
import colorama
from colorama import Fore, Back, Style
print(Fore.GREEN + 'This text is green,' + Back.BLUE + 'and now it has a blue background,' + Style.DIM + 'and now it is dark!')
As you can see, this looks a lot neater, and does not use any extraneous characters that might make the line look more complex than it is.
(Update, there is also a yellow color that isn't mentioned)
@Dablux Thanks for the suggestion, it is much more simple. However, when I do tutorials, i try to distinguish what is what so that it is a but easier for those who don't know the language that well. It is true that this is much better for inline use. Thanks again for the suggestion!
Can you do GUIs, please
@LukeShomper I did, you can check out my tkinter tutorial, or pygame tutorial for GUIs. Hope they help!
Hell yeah! Well written Leon!
@enigma_dev Thanks! Glad you liked it!
@LeonDoesCode could you join my group?
It is called Mando123