Colors in Windows text mode?

Thomas Heller thomas.heller at ion-tof.com
Fri Jul 27 14:16:02 CEST 2001


> I was wondering, is it possible to output colored text in Python while in
> Windows text/console mode? You could call it "in a DOS box", but I'm talking
> about Windows Python, not a DOS version. I vaguely recall that I have seen an
> example of this a long time ago, but I haven't been able to find it.
>
> TIA,
Well, there is Fredrik Lundh's windows console module, but this
takes over the whole console.
Here is another extension module, which makes a setColor() function
available and the 16 possible colors under their names. It allows
to change the console text color in the low byte, and the backgound color
in the high byte.

Python style license,

Thomas
--- file _color.c ---
#include "Python.h"
#include <windows.h>

static char setColor__doc__[] =
"Set the console color, return previous console color.";

static char module_doc[] = "";

static PyObject *setColor(PyObject *self, PyObject *args)
{
    int color;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE handle;

    if (!PyArg_ParseTuple(args, "i", &color))
        return NULL;
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    if (handle == INVALID_HANDLE_VALUE || !GetConsoleScreenBufferInfo(handle, &csbi)) {
        PyErr_SetString(PyExc_RuntimeError, "Cannot get console screen buffer info");
        return NULL;
    }
    if (!SetConsoleTextAttribute(handle, color)) {
        PyErr_SetString(PyExc_RuntimeError, "Cannot set console text attribute");
        return NULL;
    }
    return PyInt_FromLong(csbi.wAttributes);
}

static PyMethodDef methods[] = {
  { "setColor", setColor, METH_VARARGS,
  setColor__doc__ },
    { NULL, NULL },             /* Sentinel */
};

DL_EXPORT(void)
init_color(void)
{
    PyObject *m, *d;

    m = Py_InitModule3("_color", methods, module_doc);
    d = PyModule_GetDict(m);

    PyDict_SetItemString(d, "BLACK", PyInt_FromLong(0x00));
    PyDict_SetItemString(d, "BLUE", PyInt_FromLong(0x01));
    PyDict_SetItemString(d, "GREEN", PyInt_FromLong(0x02));
    PyDict_SetItemString(d, "CYAN", PyInt_FromLong(0x03));
    PyDict_SetItemString(d, "RED", PyInt_FromLong(0x04));
    PyDict_SetItemString(d, "MAGENTA", PyInt_FromLong(0x05));
    PyDict_SetItemString(d, "BROWN", PyInt_FromLong(0x06));
    PyDict_SetItemString(d, "WHITE", PyInt_FromLong(0x07));
    PyDict_SetItemString(d, "GRAY", PyInt_FromLong(0x08));
    PyDict_SetItemString(d, "LIGHT_BLUE", PyInt_FromLong(0x09));
    PyDict_SetItemString(d, "LIGHT_GREEN", PyInt_FromLong(0x0A));
    PyDict_SetItemString(d, "LIGHT_CYAN", PyInt_FromLong(0x0B));
    PyDict_SetItemString(d, "LIGHT_RED", PyInt_FromLong(0x0C));
    PyDict_SetItemString(d, "LIGHT_MAGENTA", PyInt_FromLong(0x0D));
    PyDict_SetItemString(d, "YELLOW", PyInt_FromLong(0x0E));
    PyDict_SetItemString(d, "BRIGHT_WHITE", PyInt_FromLong(0x0F));
}
--- EOF ---
--- setup.py ---
from distutils.core import setup, Extension

import py2exe

setup(name="_color",
      version="0.1",
      description="",
      license="Python",
      author="Thomas Heller",
      author_email = "theller at python.net",

      ext_modules = [Extension("_color",
                              sources=["extensions/_color.c"]),
                     ],

      )
--- EOF ---





More information about the Python-list mailing list