[Tutor] linux terminal coloring in python

Alan Gauld alan.gauld at btinternet.com
Sun Sep 30 00:42:25 CEST 2007


"Robert Jackson" <wolf1boy85 at yahoo.com> wrote

> I'm trying to get some pretty colored output for a Linux console
> / terminal window.  Google searches only reveal the curses module
> to colorize output.

curses means it will work for any console supporting color and curses.

> Is there a simpler way?  Curses seems to be FAR too powerful

If you know for sure what terminal you are using then you can
send control codes to the screen directly. But since color terninals
are far from standard you will need to find out what kind of terminal
you are using (eg Dec VT340 supports color as do various
Tectronics and Wyse terminals). On Linux you are probably
using a terminal emulator running in an xterm or some other
window interface. Xterm uses the terminal settings you tell
it to use in the X resources. But some more modern consoles
have their own codes so you need to look them up.

In short if you know the control codes used to change color
then it's easy to print them out. Something like:

# These codes are just made up, you need the real ones!
YELLOW = 0x23
BLUE = 0x4A
RED = 0xB2

print YELLOW, 'This is yellow', RED, 'and this is red'


But that will produce something completely different in
another console type.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list