-
PYTHON > de la couleur dans le terminal
Terminal Output in Python
Codes pour l’écran
print('\033[2J') # efface l'écran
\033 ou \x1b (hexadecimal)
Dans un shell bash \e du genre \e[31m.
[31m means "red foreground mode" (commence avec un [ et parfois se termine avec m)
[2J is the code to clear the screen
[3D will move the cursor back 3 characters
m in [31m is because we are toggling graphics mode 31 (red foreground)
CRÉER UNE CLASSE
class color: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' FIN = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' print color.WARNING + "Attention !!!" + color.FIN
RESET = "\033[0;0m" BOLD = "\033[;1m" REVERSE = "\033[;7m"
POUR WINDOWS > colorama
grâce à
colorama, on peut utiliser les codes ANSI sous Windows. Sinon, colorama ne fera rien si le terminal accepte déjà les codes ANSI (Shell Linux)pyhon -m pip install colorama
Clear the screen
import colorama colorama.init() print('\033[2J') # clear screen print(colorama.ansi.clear_screen())
Changer le text style
import colorama colorama.init() CLEAR_SCREEN = '\033[2J' RED = '\033[31m' # texte rouge RESET = '\033[0m' # retour au texte normal print(CLEAR_SCREEN + RED + 'coucou!' + RESET)
ATTENTION : Windows ne supporte pas le mode dim (voir ANSI escape code list)
codes couleurs
3x pour le texte, 4x pour le background
0 = noir
1 = rouge
2 = vert
3 = jaune
4 =bleu
5 =magenta
6 = cyan
7 = blanc
alternatives
Curses Programming with Python
Reference links
—