玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python如何在windows命令行输出彩色字符的方法

python 培训

本文Python教程源码是关于,用Python方法实现在windows命令行输出彩色字符。Python如何在windows命令行输出彩色字符的方法,需要用到的python相关模块方法有:sysos

默认python在windows命令行下输出的文字颜色都是黑白的,这也是最传统的。但如果做出特别的颜色,用以反馈特殊情况感觉会很高大尚。所以我试着写了python在windows命令行下输出彩色文字的方法。这是Python文字特效操作方法相关的技巧,给需要的朋友们一个参考。

python 在windows命令行输出彩色字符的方法

Python如何在windows命令行输出彩色字符的方法,源码如下:

import sys, os

if os.name != 'nt':
   print "在windows使用这个模块"
   sys.exit()

try:
   from ctypes import *
except:
   print 'I need module ctypes'
   sys.exit()

try:
   from win32con import *
except:
   print 'I need module win32con'
   sys.exit()

# command colors
cc_map = {
    'default'      :0,
    'black'        :1,
    'blue'         :2,
    'green'        :3,
    'cyan'         :4,
    'red'          :5,
    'magenta'      :6,
    'brown'        :7,
    'lightgray'    :8,
    'darkgray'     :9,
    'lightblue'    :10,
    'lightgreen'   :11,
    'lightcyan'    :12,
    'lightred'     :13,
    'lightmagenta' :14,
    'yellow'       :15,
    'white'        :16,
};
        
CloseHandle = windll.kernel32.CloseHandle
GetStdHandle = windll.kernel32.GetStdHandle
GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
#www.iplaypython.com

STD_OUTPUT_HANDLE = -11

class COORD(Structure):
   _fields_ = [('X', c_short),
               ('Y', c_short),
              ]

class SMALL_RECT(Structure):
   _fields_ = [('Left', c_short),
               ('Top', c_short),
               ('Right', c_short),
               ('Bottom', c_short),
              ]
               
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
   _fields_ = [('dwSize', COORD),
               ('dwCursorPosition', COORD),
               ('wAttributes', c_uint),
               ('srWindow', SMALL_RECT),
               ('dwMaximumWindowSize', COORD),
              ]

def print_cc(fore_color, back_color, text):

    if not (cc_map.has_key(fore_color) and
            cc_map.has_key(back_color)):

        #color not found
 
       print >>stderr, fore_color, back_lolor, " are invalid color strings"

        return

    #prepare
    hconsole = GetStdHandle(STD_OUTPUT_HANDLE)
    cmd_info = CONSOLE_SCREEN_BUFFER_INFO()
    GetConsoleScreenBufferInfo(hconsole, byref(cmd_info))
    old_color = cmd_info.wAttributes

    #calculate colors
    fore = cc_map[fore_color]
    if fore: fore = fore - 1
    else: fore = old_color & 0x0F
    back = cc_map[back_color]
    if back: back = (back - 1) << 4
    else: back = old_color & 0xF0

    #real output
    SetConsoleTextAttribute(hconsole, fore + back)
    print text,
    SetConsoleTextAttribute(hconsole, old_color)

if __name__ == "__main__":
    #let's print the color matrix
    #first line
    print("  Color map:")

    keys = [key for key in cc_map]

    for i in range(11, -1, -1): #12 is the max len, "lightmagenta"
        print " " * 20,

        for j in range(0, 17):
            k = keys[j]
            l = len(k)
            c = ' '
            if l > i:
                c = k[l - i - 1]

            print(" %s" % c),

        print

    #lines with color and background cresponding to i & j
  
  for fc in keys:
        print "        %12s " % fc, 

        for bc in keys:

            print_cc(fc, bc, ":)")
        print

用Python在windows命令行输出彩色字符的源码还有不足之处,有需要的朋友自己修改再使用。

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2583.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-12 19:53 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部