Python's Way of outputting colored text under Windows command line _python

Source: Internet
Author: User

The example in this article describes how Python prints color text at the Windows command line. Share to everyone for your reference. The specific analysis is as follows:

By default Python in the console output text information is black and white, if the text can be made into color output, the output will be more perfect, and cool, is not it, the following is a section of the demo code, this code encapsulates a color class used to output colored text, Just call the relevant method inside the class, it's very simple.

Copy Code code 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:
"" "Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/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_cmd_color (foreground_red | Foreground_green | Foreground_blue | foreground_intensity)
"""
BOOL = Ctypes.windll.kernel32.SetConsoleTextAttribute (handle, color)
return bool
def reset_color (self):
Self.set_cmd_color (foreground_red | Foreground_green | Foreground_blue)
def print_red_text (self, Print_text):
Self.set_cmd_color (foreground_red | foreground_intensity)
Print Print_text
Self.reset_color ()
def print_green_text (self, Print_text):
Self.set_cmd_color (Foreground_green | foreground_intensity)
Print Print_text
Self.reset_color ()
def print_blue_text (self, Print_text):
Self.set_cmd_color (Foreground_blue | foreground_intensity)
Print Print_text
Self.reset_color ()
def print_red_text_with_blue_bg (self, Print_text):
Self.set_cmd_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 your Python programming.

Related Article

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.