Terminal Colour in Python
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.
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:
One quick thing I'd like to mention about
colorama is that you can initialise it. We will be using this method:
So that each
autoreset=True or set it to
False, which it is by default.
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:
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:
Pretty cool, but are there any other colours? Yes there are! These are the colours that come with
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:
Style to do this. You'll see a gradient in the brightness from
BRIGHT. We can also reset all our colours and brightness using
Style like so:
Which is very useful to know if you have set all three and don't want to have to reset each one individually.
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!
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!
There is a much simpler way of using Colorama that makes it easier to use and read (at least to me), as follows:
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!
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??