[Tutor] PyWin32 - Library of functions to interact with windows?

Scott Nelson sirgnip at gmail.com
Tue Nov 3 20:18:48 CET 2009


Scott Nelson <sirgnip at gmail.com> wrote:
>
>  It *is* possible to color console text with Python and pywin.  But, it is
> tricky and not obvious.  I've been wondering how to do this myself and I
> recently found some C code on the web [2] that does this and I translated
> that into to Python and pywin.  It can be done in about 4 lines of Python.
>

For the sake of posterity (and the archives), I figured I'd list the "4
lines of Python" I mentioned above:

<code>
import win32console
handle = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE)
handle.SetConsoleTextAttribute(win32console.FOREGROUND_BLUE)
print 'blue text'
</code>

Beginning with this snippet, there are a number of other things you can do,
such as set the cursor position (handle.SetConsoleCursorPosition) and get
the properties of the console itself (handle.GetConsoleScreenBufferInfo).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091103/a27218e7/attachment-0001.htm>


More information about the Tutor mailing list