zoukankan      html  css  js  c++  java
  • 【转】Python实现修改Windows CMD命令行输出颜色(完全解析)

    用Python写命令行程序的时候,单一的输出颜色太单调。其实我们可以加些色彩,比如用红色表示警告,绿色表示结果正常等。网上也有几篇类似的帖子,但是没有把问题讲清楚,贴的代码也不是太清晰。这里,对Windows下CMD输出彩色进行完整的讲解和易用的代码函数库展示。

    原理解析参见:http://my.oschina.net/bluefly/blog/311209

    1、程序运行结果展示

    2、核心代码介绍

    1
    2
    3
    4
    5
    6
    # 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

    这里应该是获取输出窗口的句柄,和Windows的编程特性有关。

    关键代码其实就一句:

    1
     set_cmd_text_color(这里参数要填颜色的16进制编码)

    颜色的讲解:

    这个函数支持2位十六进制编码,比如0x12,0x03,0xef。取值都是0~f。区别在于,前一位指的是背景色,后一位指的是前景色,我们为了方便,可以给这些颜色编码定义一些有意义的别名。比如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    # Windows CMD命令行 字体颜色定义 text colors
    FOREGROUND_BLACK = 0x00 # black.
    FOREGROUND_DARKBLUE = 0x01 # dark blue.
    FOREGROUND_DARKGREEN = 0x02 # dark green.
    FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
    FOREGROUND_DARKRED = 0x04 # dark red.
    FOREGROUND_DARKPINK = 0x05 # dark pink.
    FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
    FOREGROUND_DARKWHITE = 0x07 # dark white.
    FOREGROUND_DARKGRAY = 0x08 # dark gray.
    FOREGROUND_BLUE = 0x09 # blue.
    FOREGROUND_GREEN = 0x0a # green.
    FOREGROUND_SKYBLUE = 0x0b # skyblue.
    FOREGROUND_RED = 0x0c # red.
    FOREGROUND_PINK = 0x0d # pink.
    FOREGROUND_YELLOW = 0x0e # yellow.
    FOREGROUND_WHITE = 0x0f # white.
     
     
    # Windows CMD命令行 背景颜色定义 background colors
    BACKGROUND_BLUE = 0x10 # dark blue.
    BACKGROUND_GREEN = 0x20 # dark green.
    BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
    BACKGROUND_DARKRED = 0x40 # dark red.
    BACKGROUND_DARKPINK = 0x50 # dark pink.
    BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
    BACKGROUND_DARKWHITE = 0x70 # dark white.
    BACKGROUND_DARKGRAY = 0x80 # dark gray.
    BACKGROUND_BLUE = 0x90 # blue.
    BACKGROUND_GREEN = 0xa0 # green.
    BACKGROUND_SKYBLUE = 0xb0 # skyblue.
    BACKGROUND_RED = 0xc0 # red.
    BACKGROUND_PINK = 0xd0 # pink.
    BACKGROUND_YELLOW = 0xe0 # yellow.
    BACKGROUND_WHITE = 0xf0 # white.

    注意下边这句:把不同颜色进行或操作,结果肯定还是16种颜色的一种。

    1
    set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

    3、完整代码

    我把每种颜色代码都封装成了易用的函数,可以根据需要在自己的程序里直接导入相应函数进行彩色输出。

    比如原来是 print 'hello'   现在直接 printBlue('hello') 就可以了。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    #! /usr/bin/env python
    #coding=utf-8
     
    import ctypes,sys
     
    STD_INPUT_HANDLE = -10
    STD_OUTPUT_HANDLE = -11
    STD_ERROR_HANDLE = -12
     
    # 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色
    #由于该函数的限制,应该是只有这16种,可以前景色与背景色组合。也可以几种颜色通过或运算组合,组合后还是在这16种颜色中
     
    # Windows CMD命令行 字体颜色定义 text colors
    FOREGROUND_BLACK = 0x00 # black.
    FOREGROUND_DARKBLUE = 0x01 # dark blue.
    FOREGROUND_DARKGREEN = 0x02 # dark green.
    FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
    FOREGROUND_DARKRED = 0x04 # dark red.
    FOREGROUND_DARKPINK = 0x05 # dark pink.
    FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
    FOREGROUND_DARKWHITE = 0x07 # dark white.
    FOREGROUND_DARKGRAY = 0x08 # dark gray.
    FOREGROUND_BLUE = 0x09 # blue.
    FOREGROUND_GREEN = 0x0a # green.
    FOREGROUND_SKYBLUE = 0x0b # skyblue.
    FOREGROUND_RED = 0x0c # red.
    FOREGROUND_PINK = 0x0d # pink.
    FOREGROUND_YELLOW = 0x0e # yellow.
    FOREGROUND_WHITE = 0x0f # white.
     
     
    # Windows CMD命令行 背景颜色定义 background colors
    BACKGROUND_BLUE = 0x10 # dark blue.
    BACKGROUND_GREEN = 0x20 # dark green.
    BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
    BACKGROUND_DARKRED = 0x40 # dark red.
    BACKGROUND_DARKPINK = 0x50 # dark pink.
    BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
    BACKGROUND_DARKWHITE = 0x70 # dark white.
    BACKGROUND_DARKGRAY = 0x80 # dark gray.
    BACKGROUND_BLUE = 0x90 # blue.
    BACKGROUND_GREEN = 0xa0 # green.
    BACKGROUND_SKYBLUE = 0xb0 # skyblue.
    BACKGROUND_RED = 0xc0 # red.
    BACKGROUND_PINK = 0xd0 # pink.
    BACKGROUND_YELLOW = 0xe0 # yellow.
    BACKGROUND_WHITE = 0xf0 # white.
     
     
     
    # 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)
     
    ###############################################################
     
    #暗蓝色
    #dark blue
    def printDarkBlue(mess):
        set_cmd_text_color(FOREGROUND_DARKBLUE)
        sys.stdout.write(mess)
        resetColor()
     
    #暗绿色
    #dark green
    def printDarkGreen(mess):
        set_cmd_text_color(FOREGROUND_DARKGREEN)
        sys.stdout.write(mess)
        resetColor()
     
    #暗天蓝色
    #dark sky blue
    def printDarkSkyBlue(mess):
        set_cmd_text_color(FOREGROUND_DARKSKYBLUE)
        sys.stdout.write(mess)
        resetColor()
     
    #暗红色
    #dark red
    def printDarkRed(mess):
        set_cmd_text_color(FOREGROUND_DARKRED)
        sys.stdout.write(mess)
        resetColor()
     
    #暗粉红色
    #dark pink
    def printDarkPink(mess):
        set_cmd_text_color(FOREGROUND_DARKPINK)
        sys.stdout.write(mess)
        resetColor()
     
    #暗黄色
    #dark yellow
    def printDarkYellow(mess):
        set_cmd_text_color(FOREGROUND_DARKYELLOW)
        sys.stdout.write(mess)
        resetColor()
     
    #暗白色
    #dark white
    def printDarkWhite(mess):
        set_cmd_text_color(FOREGROUND_DARKWHITE)
        sys.stdout.write(mess)
        resetColor()
     
    #暗灰色
    #dark gray
    def printDarkGray(mess):
        set_cmd_text_color(FOREGROUND_DARKGRAY)
        sys.stdout.write(mess)
        resetColor()
     
    #蓝色
    #blue
    def printBlue(mess):
        set_cmd_text_color(FOREGROUND_BLUE)
        sys.stdout.write(mess)
        resetColor()
     
    #绿色
    #green
    def printGreen(mess):
        set_cmd_text_color(FOREGROUND_GREEN)
        sys.stdout.write(mess)
        resetColor()
     
    #天蓝色
    #sky blue
    def printSkyBlue(mess):
        set_cmd_text_color(FOREGROUND_SKYBLUE)
        sys.stdout.write(mess)
        resetColor()
     
    #红色
    #red
    def printRed(mess):
        set_cmd_text_color(FOREGROUND_RED)
        sys.stdout.write(mess)
        resetColor()
     
    #粉红色
    #pink
    def printPink(mess):
        set_cmd_text_color(FOREGROUND_PINK)
        sys.stdout.write(mess)
        resetColor()
     
    #黄色
    #yellow
    def printYellow(mess):
        set_cmd_text_color(FOREGROUND_YELLOW)
        sys.stdout.write(mess)
        resetColor()
     
    #白色
    #white
    def printWhite(mess):
        set_cmd_text_color(FOREGROUND_WHITE)
        sys.stdout.write(mess)
        resetColor()
     
    ##################################################
     
    #白底黑字
    #white bkground and black text
    def printWhiteBlack(mess):
        set_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE)
        sys.stdout.write(mess)
        resetColor()
     
    #白底黑字
    #white bkground and black text
    def printWhiteBlack_2(mess):
        set_cmd_text_color(0xf0)
        sys.stdout.write(mess)
        resetColor()
     
     
    #黄底蓝字
    #white bkground and black text
    def printYellowRed(mess):
        set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)
        sys.stdout.write(mess)
        resetColor()
     
     
    ##############################################################
     
    if __name__ == '__main__':
         
        print
        printDarkBlue(u'printDarkBlue:暗蓝色文字 '.encode('gb2312'))
        printDarkGreen(u'printDarkGreen:暗绿色文字 '.encode('gb2312'))
        printDarkSkyBlue(u'printDarkSkyBlue:暗天蓝色文字 '.encode('gb2312'))
        printDarkRed(u'printDarkRed:暗红色文字 '.encode('gb2312'))
        printDarkPink(u'printDarkPink:暗粉红色文字 '.encode('gb2312'))
        printDarkYellow(u'printDarkYellow:暗黄色文字 '.encode('gb2312'))
        printDarkWhite(u'printDarkWhite:暗白色文字 '.encode('gb2312'))
        printDarkGray(u'printDarkGray:暗灰色文字 '.encode('gb2312'))
        printBlue(u'printBlue:蓝色文字 '.encode('gb2312'))
        printGreen(u'printGreen:绿色文字 '.encode('gb2312'))
        printSkyBlue(u'printSkyBlue:天蓝色文字 '.encode('gb2312'))
        printRed(u'printRed:红色文字 '.encode('gb2312'))
        printPink(u'printPink:粉红色文字 '.encode('gb2312'))
        printYellow(u'printYellow:黄色文字 '.encode('gb2312'))
        printWhite(u'printWhite:白色文字 '.encode('gb2312'))
         
        printWhiteBlack(u'printWhiteBlack:白底黑字输出 '.encode('gb2312'))
        printWhiteBlack_2(u'printWhiteBlack_2:白底黑字输出(直接传入16进制参数) '.encode('gb2312'))
        printYellowRed(u'printYellowRed:黄底红字输出 '.encode('gb2312'))

     

  • 相关阅读:
    bzoj1415 NOI2005聪聪和可可
    Tyvj1952 Easy
    poj2096 Collecting Bugs
    COGS 1489玩纸牌
    COGS1487 麻球繁衍
    cf 261B.Maxim and Restaurant
    cf 223B.Two Strings
    cf 609E.Minimum spanning tree for each edge
    cf 187B.AlgoRace
    cf 760B.Frodo and pillows
  • 原文地址:https://www.cnblogs.com/hester/p/5144694.html
Copyright © 2011-2022 走看看