Python/IDLE - text in different colours

Bill Davy Bill at SynectixLtd.com
Wed Jun 29 12:16:17 EDT 2005


OK, I (sort of) tried that.  Used chr() to avoid issues of which editor and 
rant the following:

import sys

ESC = chr(27)
DarkRed = ESC + "[31;2m"
ResetColour = ESC + "[0m"

print "Initial colour"

sys.stdout.write(DarkRed) ; sys.stdout.flush()

print "Is this dark red?"

sys.stdout.write(ResetColour) ; sys.stdout.flush()

print "Final colour"

The output (in blue, using IDLE) was:

Initial colour
Is this dark red?
Final colour

So, have I missed soemthing?  By the way, in the output there is a little 
square box before the [ in the last two lines.  Does the window Idle sets up 
emulate VT100?

Hey ho, but many thanks.  My user will just have to strain his eyes.
Bill

PS Thanks for the URL.  Interesting.


"TouTaTis" <toutatis at xsFOURall.nl> wrote in message 
news:Xns9684B0E245DE6toutatisxsFOURallnl at 194.109.133.242...
> "Bill Davy" <Bill at SynectixLtd.com> wrote in
> news:d9rfb9$ob7$1 at nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:
>
>> To make life easier for my users, I'd like to colour my prompt string
>> (as handed to raw_input()) a different colour to that produced by
>> print.  I'm using Python 2.4.1 and IDLE 1.1.1 on Windows XP.  Is it
>> possible, and if so, how?
>> tia,
>>     Bill
>>
>>
>
> Communicating with a Program
>
> Say we want the shell to distinguish more clearly, the output of external
> programs from the input prompt, the commands, and the shell feedback. We
> want the output of external programs to be indented and displayed in a
> different colour than the other text.
>
> Setting the colour of the text is fairly easy using ANSI terminal escape
> sequences. For instance, to set the text colour to dark red, write "<Esc>
> [31;2m" to the terminal (where <Esc> is the escape code - in emacs use
> "C-q ESC" to write <Esc>). We can reset the output colour using "<Esc>
> 0m".
>
> Printing the output of external programs in dark red, we can do using the
> execute() function:
>
> def runCommand(command):
>    print 'Running:', command
>
>    # set output colour:
>    sys.stdout.write("<Esc>[31;2m") ; sys.stdout.flush()
>
>    os.system(command)
>
>    # reset output colour
>    sys.stdout.write("<Esc>[0m")
>
> (Here we need to flush the stdout file to make sure that the escape code
> is written to the terminal before the output of the program)
>
> http://www.daimi.au.dk/~mailund/scripting2005/lecture-notes/process-management.html
> 





More information about the Python-list mailing list