Connecting Tech Pros Worldwide Forums | Help | Site Map

Python/IDLE - text in different colours

Bill Davy
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Nathan Pinno
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Python/IDLE - text in different colours




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" <Bill@SynectixLtd.com> wrote in message
news:d9rfb9$ob7$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
> To make life easier for my users, I'd like to colour my prompt string[/color]
(as[color=blue]
> handed to raw_input()) a different colour to that produced by print.[/color]
I'm[color=blue]
> using Python 2.4.1 and IDLE 1.1.1 on Windows XP. Is it possible, and if[/color]
so,[color=blue]
> how?
> tia,
> Bill
>
>[/color]



--


----------------------------------------------------------------
Posted via UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Bill Davy
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Python/IDLE - text in different colours


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" <falcon3166@hotmail.com> wrote in message
news:42c19a13$0$7025$b9fe7a78@news.usenetrevolutio n.com...[color=blue]
>
>
> 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" <Bill@SynectixLtd.com> wrote in message
> news:d9rfb9$ob7$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...[color=green]
> > To make life easier for my users, I'd like to colour my prompt string[/color]
> (as[color=green]
> > handed to raw_input()) a different colour to that produced by print.[/color]
> I'm[color=green]
> > using Python 2.4.1 and IDLE 1.1.1 on Windows XP. Is it possible, and[/color]
> if
> so,[color=green]
> > how?
> > tia,
> > Bill
> >
> >[/color]
>
>
>
> --
>
>
> ----------------------------------------------------------------
> Posted via UsenetRevolution.com - Revolutionary Usenet
> ** HIGH RETENTION ** Specializing in Large Binaries Downloads **
> http://www.UsenetRevolution.com[/color]


TouTaTis
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Python/IDLE - text in different colours


"Bill Davy" <Bill@SynectixLtd.com> wrote in
news:d9rfb9$ob7$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:
[color=blue]
> 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
>
>[/color]

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

Bill Davy
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Python/IDLE - text in different colours


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@xsFOURall.nl> wrote in message
news:Xns9684B0E245DE6toutatisxsFOURallnl@194.109.1 33.242...[color=blue]
> "Bill Davy" <Bill@SynectixLtd.com> wrote in
> news:d9rfb9$ob7$1@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com:
>[color=green]
>> 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
>>
>>[/color]
>
> 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
>[/color]


Closed Thread


Similar Python bytes