|
Description:
A simple function to change the background/foreground color of characters written to a Windows console (command line). Requires ctypes, which is included with Python version 2.5 or later.
Source: Text Source
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12
FOREGROUND_BLUE = 0x01
FOREGROUND_GREEN= 0x02
FOREGROUND_RED = 0x04
FOREGROUND_INTENSITY = 0x08
BACKGROUND_BLUE = 0x10
BACKGROUND_GREEN= 0x20
BACKGROUND_RED = 0x40
BACKGROUND_INTENSITY = 0x80
import ctypes
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def set_color(color, handle=std_out_handle):
"""(color) -> BOOL
Example: set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
"""
bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return bool
Discussion:
See link for information on the two Windows APIs called by this code.
|