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:
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!
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: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
print
statement has its colours reset. If you want the colours to be kept, then you can remove theautoreset=True
or set it toFalse
, 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 howcolorama
works. So let's begin by making theHello,
look red, and theWorld!
look blue. We will then printHello, 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
colorama
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:
We use
Style
to do this. You'll see a gradient in the brightness fromDIM
toNORMAL
toBRIGHT
. We can also reset all our colours and brightness usingStyle
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.
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!
@enigma_dev Thanks! Glad you liked it!