Skip to content

Instantly share code, notes, and snippets.

@Xyene
Created November 6, 2013 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xyene/7328921 to your computer and use it in GitHub Desktop.
Save Xyene/7328921 to your computer and use it in GitHub Desktop.
A bunch of methods to make your Windows Pythons console apps more flashy.
import ctypes
from ctypes.wintypes import *
from ctypes import *
# Standard colours
FOREGROUND_BLUE = 0x0001
FOREGROUND_GREEN = 0x0002
FOREGROUND_RED = 0x0004
FOREGROUND_INTENSITY = 0x0008
BACKGROUND_BLUE = 0x0010
BACKGROUND_GREEN = 0x0020
BACKGROUND_RED = 0x0040
BACKGROUND_INTENSITY = 0x0080
# Composite colours
FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN
FOREGROUND_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
BACKGROUND_YELLOW = BACKGROUND_RED | BACKGROUND_GREEN
BACKGROUND_WHITE = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
_STDIO_HANDLE = ctypes.windll.kernel32.GetStdHandle(-11)
_WriteFile = ctypes.windll.kernel32.WriteFile
_SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
_buf = []
_dw = DWORD()
_color = 0
def flushc():
text = ''.join(_buf)
if _WriteFile(_STDIO_HANDLE, text, len(text), byref(_dw), None) == 0:
print "StdIO write failed with %s." % ctypes.windll.kernel32.GetLastError()
del _buf[:]
def colorc(color):
global _color
if _color != color:
flushc()
_SetConsoleTextAttribute(_STDIO_HANDLE, color)
_color = color
def cursor_pos(x, y):
ctypes.windll.kernel32.SetConsoleCursorPosition(_STDIO_HANDLE, _COORD(x, y))
class CONSOLE_FONT_INFOEX(Structure):
_fields_ = [("cbSize", c_ulong),
("nFont", c_ulong),
("dwFontSize", _COORD),
("FontFamily", c_uint),
("FontWeight", c_uint),
("FaceName", c_wchar * 32)]
def set_font(font):
if not ctypes.windll.kernel32.SetCurrentConsoleFontEx(_STDIO_HANDLE, c_long(False), ctypes.pointer(font)):
print "Failed to set font."
def get_font():
struct = CONSOLE_FONT_INFOEX()
struct.cbSize = sizeof(CONSOLE_FONT_INFOEX)
if not ctypes.windll.kernel32.GetCurrentConsoleFontEx(_STDIO_HANDLE, c_long(False), ctypes.pointer(struct)):
print "Failed to fetch font."
return struct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment