By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Python/IDLE - text in different colours

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


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
"Bill Davy" <Bi**@SynectixLtd.com> wrote in message
news:d9**********@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


--
----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Jul 19 '05 #2

P: n/a
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"

"Nathan Pinno" <fa********@hotmail.com> wrote in message
news:42**********************@news.usenetrevolutio n.com...


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
"Bill Davy" <Bi**@SynectixLtd.com> wrote in message
news:d9**********@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
>
>


--
----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com

Jul 19 '05 #3

P: n/a
"Bill Davy" <Bi**@SynectixLtd.com> wrote in
news:d9**********@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/scri...notes/process-
management.html

Jul 19 '05 #4

P: n/a
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" <to******@xsFOURall.nl> wrote in message
news:Xn*********************************@194.109.1 33.242...
"Bill Davy" <Bi**@SynectixLtd.com> wrote in
news:d9**********@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/scri...anagement.html

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.