Python realizes console output color font

The python console outputs colored fonts for your reference. The specific content is as follows

import ctypes, sys

STD_INPUT_HANDLE =-10
STD_OUTPUT_HANDLE =-11
STD_ERROR_HANDLE =-12

# Font color definition text colors
FOREGROUND_BLUE =0x09 # blue.
FOREGROUND_GREEN =0x0a # green.
FOREGROUND_RED =0x0c # red.
FOREGROUND_YELLOW =0x0e # yellow.

# Background colors
BACKGROUND_YELLOW =0xe0 # yellow.

# get handle
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

def set_cmd_text_color(color, handle=std_out_handle):
 Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)return Bool

# reset white
def resetColor():set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

# green
def printGreen(mess):set_cmd_text_color(FOREGROUND_GREEN)
 sys.stdout.write(mess +'\n')resetColor()

# red
def printRed(mess):set_cmd_text_color(FOREGROUND_RED)
 sys.stdout.write(mess +'\n')resetColor()

# yellow
def printYellow(mess):set_cmd_text_color(FOREGROUND_YELLOW)
 sys.stdout.write(mess +'\n')resetColor()

# white bkground and black text
def printYellowRed(mess):set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)
 sys.stdout.write(mess +'\n')resetColor()

use

printGreen("Hello world!")

By the way, put out the graphics for everyone to use

replyContent = u''''_(\ _      ___
    .-"`"(\    _.""``"-.
 /  ``-._  _.-"   `\__
 66)`- .__.- '  `", /`;-`/,|()//`         |
   `---`"~``\         |
     \         |
  \ \ /   /
  /`,,||// "-.|||/'
  // |  /,__ |/`\ 
 ljs / /' | / `"'\  ( \ 
 __ //' ||`\  \ \ 
 \ /  | |   `\ \ \ 
 `- , /  / |   /  |-"``"""^^^   `^^""""`

  ------------------------------------------------'''

printGreen(replyContent)

Note: The test does not display color when printing in the idea development tool, but in the Linux console and cmd console, the fonts are all colored.

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python realizes console output color font
Python realizes stitching pictures
Python realizes online translation
Python realizes tank battle
Python3 realizes airplane war game
How does Python output integers
Python realizes apple eating game
Python realizes the guessing game
Python realizes 3D map visualization
Python realizes face sign-in system
Python output mathematical symbols example