Python outputs color text in windows command lines,

Source: Internet
Author: User

Python outputs color text in windows command lines,

This example describes how to output color text in python under the windows command line. Share it with you for your reference. The specific analysis is as follows:

By default, the text information output by python on the console is both black and white. If you can make the text into a color output, the output will be more perfect and cool, right, the following is a Demo code. This Code encapsulates a color class to output the color text. You only need to call the related methods in the class, which is very simple.
Copy codeThe Code is as follows :#! /Usr/bin/env python
# Encoding: UTF-8
Import ctypes
STD_INPUT_HANDLE =-10
STD_OUTPUT_HANDLE =-11
STD_ERROR_HANDLE =-12
FOREGROUND_BLACK = 0x0
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN = 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN = 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.
Class Color:
'''See http://msdn.microsoft.com/library/default.asp? Url =/library/en-us/winprog/windows_api_reference.asp
For information on Windows APIs.-www.jb51.net '''
Std_out_handle = ctypes. windll. kernel32.GetStdHandle (STD_OUTPUT_HANDLE)
Def set_cmd_color (self, color, handle = std_out_handle ):
"(Color)-> bit
Example: set_0000_color (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
"""
Bool = ctypes. windll. kernel32.SetConsoleTextAttribute (handle, color)
Return bool
Def reset_color (self ):
Self. set_pai_color (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
Def print_red_text (self, print_text ):
Self. set_pai_color (FOREGROUND_RED | FOREGROUND_INTENSITY)
Print print_text
Self. reset_color ()
Def print_green_text (self, print_text ):
Self. set_pai_color (FOREGROUND_GREEN | FOREGROUND_INTENSITY)
Print print_text
Self. reset_color ()
Def print_blue_text (self, print_text ):
Self. set_pai_color (FOREGROUND_BLUE | FOREGROUND_INTENSITY)
Print print_text
Self. reset_color ()
Def print_red_text_with_blue_bg (self, print_text ):
Self. set_assist_color (FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
Print print_text
Self. reset_color ()
If _ name _ = "_ main __":
Clr = Color ()
Clr. print_red_text ('red ')
Clr. print_green_text ('green ')
Clr. print_blue_text ('blue ')
Clr. print_red_text_with_blue_bg ('background ')

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.