2012-09-09 69 views
1

this question我学会了如何为Python着色。我想出了所有的颜色代码,不用担心。
无论如何,对我有效的答案是orip的ctypes。每次我想为文本着色时,必须输入ctypes.windll.kernel32.SetConsoleTextAttribute(handle, AQUA)有点累人。有没有办法将它转换成函数?我不知道如何通过函数发送变量,也不知道如何实现它们,即使我做到了。
在此先感谢! -ghostmancer 对我来说重要的是它对我有用 - 我不打算把我的剧本带走。 我的颜色:Python中的颜色功能

BLACK = 0x0000 
BLUE = 0x0001 
GREEN = 0x0002 
RED = 0x0004 
PURPLE = 0x0005 
YELLOW = 0x0006 
WHITE = 0x0007 
GRAY = 0x0008 
GREY = 0x0008 
AQUA = 0x0009 #Very Blue 
+0

难道不应该是0x3?和轻型版本的0xb? –

回答

3

嗯......如果我的理解对不对......

def a_func(handle,color): 
    ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) 

a_func(handle,AQUA) 

甚至更​​好

colorFunc = ctypes.windll.kernel32.SetConsoleTextAttribute 
colorFunc(handle,AQUA) 
+0

第二个例子非常好 - 谢谢! – Andrey

1

一种方法是

def textcolor(handle, color): 
    ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) 

你打电话,像这样:

textcolor(handle, AQUA) 
0

您可以使用:

f=lambda handle,color:ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color) 

,并呼吁f(<Actual handle object>, <color>)不管你想要的。 例如f(handle, AQUA)将所需的呼叫

2

无需创建deflambda一个新的功能,只需用长名称分配功能,较短的名称,e.g:

textcolor = ctypes.windll.kernel32.SetConsoleTextAttribute 
textcolor(handle, color) 
+0

+1这是最好的方法。 –