Reputation: 859
From this question I learned how to color Python. I figured out all the color codes, don't worry.
Anyway, the answer that worked for me was the ctypes one by orip. It's a bit tiresome to have to have to type ctypes.windll.kernel32.SetConsoleTextAttribute(handle, AQUA)
every time I want to color text. Is there a way to convert it into a function? I'm not sure how to send variables through functions, and not sure how to implement them, even if I did.
Thanks in advance! -ghostmancer
All that matters for me is that it works for me - I'm not planning to give my script away.
My colors:
BLACK = 0x0000
BLUE = 0x0001
GREEN = 0x0002
RED = 0x0004
PURPLE = 0x0005
YELLOW = 0x0006
WHITE = 0x0007
GRAY = 0x0008
GREY = 0x0008
AQUA = 0x0009 #Very Blue
Upvotes: 1
Views: 10719
Reputation: 3467
Because I see the variable 'handle' everywhere without being defined and for anyone who wonders, here is a way to get it, as far as stdout is concerned, so that we can used it with ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
:
STD_OUTPUT_HANDLE = -11
handle = ctypes.windll.kernel32.GetStdHandle(-STD_OUTPUT_HANDLE)
Upvotes: 0
Reputation: 39608
no need to create a new function with def
or lambda
, just assign the function with a long name to a shorter name, e.g:
textcolor = ctypes.windll.kernel32.SetConsoleTextAttribute
textcolor(handle, color)
Upvotes: 2
Reputation: 2641
You can use:
f=lambda handle,color:ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
and, call f(<Actual handle object>, <color>)
wherever you want.
e.g. f(handle, AQUA)
would be the required call
Upvotes: 0
Reputation: 114048
ummm... if i understand right ...
def a_func(handle,color):
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
a_func(handle,AQUA)
or even better
colorFunc = ctypes.windll.kernel32.SetConsoleTextAttribute
colorFunc(handle,AQUA)
Upvotes: 3
Reputation: 88468
One way is
def textcolor(handle, color):
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
which you call like so:
textcolor(handle, AQUA)
Upvotes: 1