在Windows 10的cmd输出彩色文本
2021/12/10 7:46:38
本文主要是介绍在Windows 10的cmd输出彩色文本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# termcolor outputs stuff like '\033[0m'. It doesn't work in Windows 10's cmd. import sys from ctypes import * user32 = CDLL('user32.dll'); kernel32 = CDLL('kernel32.dll') # https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute # HANDLE WINAPI GetStdHandle(DWORD nStdHandle) # BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes) # WINCON.H: #define FOREGROUND_BLUE 0x0001 // text color contains blue. #define FOREGROUND_GREEN 0x0002 // text color contains green. #define FOREGROUND_RED 0x0004 // text color contains red. #define FOREGROUND_INTENSITY 0x0008 // text color is intensified. #define BACKGROUND_BLUE 0x0010 // background color contains blue. #define BACKGROUND_GREEN 0x0020 // background color contains green. #define BACKGROUND_RED 0x0040 // background color contains red. #define BACKGROUND_INTENSITY 0x0080 // background color is intensified. STD_OUTPUT_HANDLE = -11 hStdOut = kernel32.GetStdHandle(STD_OUTPUT_HANDLE) class Color: def __init__(m, fg_str_or_color, bg = ''): if isinstance(fg_str_or_color, int): sys.stdout.flush() kernel32.SetConsoleTextAttribute(hStdOut, fg_str_or_color) return def f(s): t = { 'b':1, 'B':8+1, 'g':2, 'G':8+2, 'r':4, 'R':8+4 } d = 0 for c in s: d |= t.get(c, 0) #print(s, hex(d)) return d m.__init__(f(fg_str_or_color) | (f(bg) << 4)) if __name__ == '__main__': Color('G'); print('Green ', end=''); Color('rgb'); print('gray') Color('RGB', 'G'); print('Hello'), Color('rgb'), print('world') # 如果有人想做个cmdcolor包,请把代码拿去自便。甚至可以扩展为WFP - Windows Fun Pack :-) # 比如一个线程空白DialogBox,另一个干事情。GetWindowDC()后就可以画画了。哪怕GetDC(NULL) # 在屏幕上画呢。pip install pywin32; D:\Python39\Lib\site-packages\PyWin32.chm # 简历里一句"开发过一个python wheel,在github上有n颗星"?I admit我爱幻想。 # win32console里一堆函数,如SetConsoleTitle,就是没有SetConsoleTextAttribute。 # 四子棋: https://www.cnblogs.com/funwithwords/p/15626636.html # 画线和实心椭圆…… GDI # # Copyright (c) Some Guy # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # Author: Some Guy <someguy@somewhere>
这篇关于在Windows 10的cmd输出彩色文本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享
- 2024-07-04make后如何升级ssh-icode9专业技术文章分享
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享