Python/IDLE - text in different colours

B

Bill Davy

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
 
N

Nathan Pinno

Bill.

The way is the click on view, then click script checker, or something like
that. It will color code the text for you.

Nathan
 
B

Bill Davy

Thank you Nathan, but that does not quite address my question. I want to
have code in Python so

make_the_prompt_string(Red)
make_print_output(Green)
while True:
s = raw_input("This prompt (which is really several lines long) will be
in red: ")
Foo(s)
print "And the result is in Green so the user can see it"
 
T

TouTaTis

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
 
B

Bill Davy

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,308
Members
47,955
Latest member
DarciAntho

Latest Threads

Top