Console color issue

Question:

I am writing a python program with a header that is meant to be colored blue.
I am using the art library to generate text and some color codes but things just don’t make sense.

Here is the code for the program font-test.py

from art import *
import ctypes
kernel32 = ctypes.WinDLL('kernel32')
hStdOut = kernel32.GetStdHandle(-11)
mode = ctypes.c_ulong()
kernel32.GetConsoleMode(hStdOut, ctypes.byref(mode))
mode.value |= 4
kernel32.SetConsoleMode(hStdOut, mode)


class bcolors:
    HEADER = '33[95m'
    OKBLUE = '33[94m'
    OKCYAN = '33[96m'
    OKGREEN = '33[92m'
    WARNING = '33[93m'
    FAIL = '33[91m'
    ENDC = '33[0m'
    BOLD = '33[1m'
    UNDERLINE = '33[4m'

Art=text2art(f"{bcolors.OKBLUE}COOL HEADER{bcolors.ENDC}",font='graffiti',chr_ignore=True) # Return ASCII text with block font
print(Art)

This is the output from running >> python font-test.py
output
I expected it to simply print the text "COOL HEADER" in blue.

Asked By: Daniel Fraser

||

Answers:

With the help of text2art you can generate the string. If you want to print this string in a different color, you can do this in the print() function, as shown below:

Art=text2art("COOL HEADER",font='graffiti',chr_ignore=True)
print(bcolors.OKBLUE + Art + bcolors.ENDC)
Answered By: Tim_08
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.