[Idle-dev] coloring letters in output idle window

Guido van Rossum gvanrossum at gmail.com
Thu Dec 16 17:32:46 CET 2004


>  Is there a way to specify the text color using Python 2.3.4 or Idle 1.0.3 
> ? 
> 
> I'm using python 2.3.4 with idle 1.0.3 to write some encryption programs and
> want to print the plaintext in one color and the ciphertext in a different
> color. 

Python itself doesn't have this option. In IDLE,  a hack would be to
switch between writing to sys.stdout and sys.stderr -- these are
displayed in different colors. But this only works in IDLE, and your
program would print everything in the same boring color on all other
platforms (plus, the interspersing of stdout and stderr depends on the
buffering options for both files unless you use flush() a lot).

> Also it would be nice when using regular expressions to color matches a
> different color than the text that doesn't match.  Can different colors be
> sent to the Idle output window? 

In general, when you find this need, you should consider building a
real GUI using Tkinter (the same toolkit used to create IDLE) or
wxPython, but those have serious learning curves. I find Fredrik
Lundh's tutorials and examples on effbot.org very helpful; there's
also a book, Python and Tkinter Programming by John E. Grayson, which
you may find second-hand on the web.

If you're on Unix (including Linux), the curses module will let you do
this; it also has a bit of a learning curve but not quite as much as
the typical GUI toolkit.

Most terminal emulators (but not IDLE) also support using escape
sequences for specifying colors (this is how curses works). Googling
for ANSI escapes should get you the basic idea (ESC is "\033" in
Python). This should work on Windows in CMD windows and on Unix in
most terminal programs (e.g. xterm). I think support in remote
terminal programs (e.g. putty on Windows) is spotty; you might end up
with only being able to choose between regular and bold.

Good luck!

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the IDLE-dev mailing list