Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Python - Colour of output text

 
Thread Tools Search this Thread
Old 07-09-2009, 06:46 PM   #1
Default Colour of output text


Hi,

I would like to learn a way of changing the colour of a particular
part of the output text. I've tried the following:

import os
os.system("color 17")
print "This should be white on blue"

But that command changes the colour of ALL the text and the whole
background. What i'm trying to do is simple: Change a single part (A
letter, a word, a phrase) of the output text.

Is there a way of doing this?

Thanks in advance,

~~A.Rosslyn


Alex Rosslyn
  Reply With Quote
Old 07-09-2009, 06:53 PM   #2
Tim Harig
 
Posts: n/a
Default Re: Colour of output text
On 2009-07-09, Alex Rosslyn <> wrote:
> I would like to learn a way of changing the colour of a particular
> part of the output text. I've tried the following


http://catb.org/esr/faqs/smart-questions.html

> import os
> os.system("color 17")
> print "This should be white on blue"


I assume that you are on Windows?

> But that command changes the colour of ALL the text and the whole
> background. What i'm trying to do is simple: Change a single part (A
> letter, a word, a phrase) of the output text.


If you are on Windows, then there is an API accessing the Windows Console:

http://msdn.microsoft.com/en-us/libr...10(VS.85).aspx

On Unix operating systems this would be done through the curses interface:

http://docs.python.org/library/curses.html


Tim Harig
  Reply With Quote
Old 07-10-2009, 10:23 AM   #3
garabik-news-2005-05@kassiopeia.juls.savba.sk
 
Posts: n/a
Default Re: Colour of output text
Tim Harig <> wrote:
> On 2009-07-09, Alex Rosslyn <> wrote:
>> I would like to learn a way of changing the colour of a particular
>> part of the output text. I've tried the following


> On Unix operating systems this would be done through the curses interface:
>
> http://docs.python.org/library/curses.html


Or using ANSI colour codes:

colours = {
'none' : "",
'default' : "\033[0m",
'bold' : "\033[1m",
'underline' : "\033[4m",
'blink' : "\033[5m",
'reverse' : "\033[7m",
'concealed' : "\033[8m",

'black' : "\033[30m",
'red' : "\033[31m",
'green' : "\033[32m",
'yellow' : "\033[33m",
'blue' : "\033[34m",
'magenta' : "\033[35m",
'cyan' : "\033[36m",
'white' : "\033[37m",

'on_black' : "\033[40m",
'on_red' : "\033[41m",
'on_green' : "\033[42m",
'on_yellow' : "\033[43m",
'on_blue' : "\033[44m",
'on_magenta' : "\033[45m",
'on_cyan' : "\033[46m",
'on_white' : "\033[47m",

'beep' : "\007",

# non-standard attributes, supported by some terminals
'dark' : "\033[2m",
'italic' : "\033[3m",
'rapidblink' : "\033[6m",
'strikethrough': "\033[9m",
}

print colours['red'], 'this is red', colours['blue'], 'blue', colours['on_green'], 'and with a green background', colours['default']

--
-----------------------------------------------------------
| Radovan GarabĂ*k http://kassiopeia.juls.savba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!


garabik-news-2005-05@kassiopeia.juls.savba.sk
  Reply With Quote
Old 07-10-2009, 03:05 PM   #4
Tim Harig
 
Posts: n/a
Default Re: Colour of output text
On 2009-07-10, garabik-news-2005- <garabik-news-2005-> wrote:
> Tim Harig <> wrote:
>> On 2009-07-09, Alex Rosslyn <> wrote:
>>> I would like to learn a way of changing the colour of a particular
>>> part of the output text. I've tried the following

>> On Unix operating systems this would be done through the curses interface:
>> http://docs.python.org/library/curses.html

> Or using ANSI colour codes:


Which will only work for ANSI terminals.


Tim Harig
  Reply With Quote
Old 07-10-2009, 08:11 PM   #5
Chris Rebert
 
Posts: n/a
Default Re: Colour of output text
On Fri, Jul 10, 2009 at 2:23 AM,
<garabik-news-2005-> wrote:
> Tim Harig <> wrote:
>> On 2009-07-09, Alex Rosslyn <> wrote:
>>> I would like to learn a way of changing the colour of a particular
>>> part of the output text. I've tried the following

>
>> On Unix operating systems this would be done through the curses interface:
>>
>> http://docs.python.org/library/curses.html

>
> Or using ANSI colour codes:
>
> colours = {

<snip>
> Â* Â* Â* Â* Â* Â*'beep' Â* Â* Â* : Â* Â*"\007",


Sound is a color? Maybe if you have synaesthesia...

Cheers,
Chris
--
http://blog.rebertia.com


Chris Rebert
  Reply With Quote
Old 07-11-2009, 07:24 AM   #6
Nobody
 
Posts: n/a
Default Re: Colour of output text
On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote:

>>> I would like to learn a way of changing the colour of a particular
>>> part of the output text. I've tried the following

>
>> On Unix operating systems this would be done through the curses interface:
>>
>> http://docs.python.org/library/curses.html

>
> Or using ANSI colour codes:
>
> colours = {
> 'none' : "",
> 'default' : "\033[0m",
> 'bold' : "\033[1m",


[snip]

> # non-standard attributes, supported by some terminals


This comment should have appeared immediately after "none"

Hard-coding control/escape sequences is just lame. Use the curses modules
to obtain the correct sequences for the terminal.



Nobody
  Reply With Quote
Old 07-15-2009, 04:03 PM   #7
Jean-Michel Pichavant
 
Posts: n/a
Default Re: Colour of output text
Nobody wrote:
> On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote:
>
>
>>>> I would like to learn a way of changing the colour of a particular
>>>> part of the output text. I've tried the following
>>>>
>>> On Unix operating systems this would be done through the curses interface:
>>>
>>> http://docs.python.org/library/curses.html
>>>

>> Or using ANSI colour codes:
>>
>> colours = {
>> 'none' : "",
>> 'default' : "\033[0m",
>> 'bold' : "\033[1m",
>>

>
> [snip]
>
>
>> # non-standard attributes, supported by some terminals
>>

>
> This comment should have appeared immediately after "none"
>
> Hard-coding control/escape sequences is just lame. Use the curses modules
> to obtain the correct sequences for the terminal.
>
>

As the OP I'm really interested in doing so. I currently have all my
colors hard-coded.
Now It may be lame but as soon as I call curses.initscr(), it's just
messing up with my terminal, moreover I didn't figure out how to "print
'hello'" using curses color codes.
Anyone has an example ? I'm pretty sure it may fit in one line.

JM




Jean-Michel Pichavant
  Reply With Quote
Old 07-15-2009, 11:31 PM   #8
Nobody
 
Posts: n/a
Default Re: Colour of output text
On Wed, 15 Jul 2009 17:03:30 +0200, Jean-Michel Pichavant wrote:

>> Hard-coding control/escape sequences is just lame. Use the curses modules
>> to obtain the correct sequences for the terminal.
>>
>>

> As the OP I'm really interested in doing so. I currently have all my
> colors hard-coded.
> Now It may be lame but as soon as I call curses.initscr(), it's just
> messing up with my terminal,


Use curses.setupterm() to locate and parse the terminfo/termcap entry
without entering "curses mode". Most curses functions won't work without
calling initscr(), but the terminfo functions will.

> moreover I didn't figure out how to "print
> 'hello'" using curses color codes.
> Anyone has an example ? I'm pretty sure it may fit in one line.


#!/usr/bin/env python
import curses
curses.setupterm()
setaf = curses.tigetstr('setaf')
if not setaf:
setaf = ''
print (curses.tparm(setaf,1) + "hello, " +
curses.tparm(setaf,2) + "world" +
curses.tparm(setaf,0))




Nobody
  Reply With Quote
Old 08-02-2009, 02:54 PM   #9
Albert van der Horst
 
Posts: n/a
Default Re: Colour of output text
In article <mailman.3163.1247670223.8015.python->,
Jean-Michel Pichavant <> wrote:
>Nobody wrote:
>> On Fri, 10 Jul 2009 09:23:54 +0000, garabik-news-2005-05 wrote:
>>
>>
>>>>> I would like to learn a way of changing the colour of a particular
>>>>> part of the output text. I've tried the following
>>>>>
>>>> On Unix operating systems this would be done through the curses interface:
>>>>
>>>> http://docs.python.org/library/curses.html
>>>>
>>> Or using ANSI colour codes:
>>>
>>> colours = {
>>> 'none' : "",
>>> 'default' : "\033[0m",
>>> 'bold' : "\033[1m",
>>>

>>
>> [snip]
>>
>>
>>> # non-standard attributes, supported by some terminals
>>>

>>
>> This comment should have appeared immediately after "none"
>>
>> Hard-coding control/escape sequences is just lame. Use the curses modules
>> to obtain the correct sequences for the terminal.
>>
>>

>As the OP I'm really interested in doing so. I currently have all my
>colors hard-coded.
>Now It may be lame but as soon as I call curses.initscr(), it's just
>messing up with my terminal, moreover I didn't figure out how to "print
>'hello'" using curses color codes.
>Anyone has an example ? I'm pretty sure it may fit in one line.


In general initscr() ought to work. It fails if it has no/a wrong
idea of what your terminal is.

In the same shell as you run run your program type

set | grep -i term

Now some entry should show up, e.g.
TERM=xterm

infocmp $TERM

should fetch information about your terminal, from the same source
as curses does.

Possible problems are:
- your operating system/configurations lies to curses about the terminal
or your terminal is not specified at all
- curses has not been properly installed and cannot find the database

Groetjes Albert


>
>JM
>
>



--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst



Albert van der Horst
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CISCO 1801 DNS problem marsav Hardware 2 07-05-2009 11:41 PM
Post-Route Simulation does not give output for the first clock cycle Options velocityreviews Software 0 04-17-2007 05:47 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 10:49 PM
Which is not a colour printer question? Will Hay A+ Certification 3 02-28-2004 09:49 PM
Panasonic S25 DVD player w/o S-video output - will its replacement have S-video? Mark DVD Video 1 02-11-2004 04:19 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46