Printing with colors in a portable way

R

Robert Dailey

Anyone know of a way to print text in Python 3.1 with colors in a
portable way? In other words, I should be able to do something like
this:

print_color( "This is my text", COLOR_BLUE )

And this should be portable (i.e. it should work on Linux, Mac,
Windows).
 
R

Rhodri James

Anyone know of a way to print text in Python 3.1 with colors in a
portable way? In other words, I should be able to do something like
this:

print_color( "This is my text", COLOR_BLUE )

And this should be portable (i.e. it should work on Linux, Mac,
Windows).

....in an xterm, over a telnet connection, via VNC...

There's no portable way of doing this in any language, since it's
up to the terminal emulator exactly what it does with what incoming
bytes. Some respect the ANSI colour codes, some don't; that's about
all that you can say.
 
N

Nobody

Anyone know of a way to print text in Python 3.1 with colors in a
portable way? In other words, I should be able to do something like
this:

print_color( "This is my text", COLOR_BLUE )

And this should be portable (i.e. it should work on Linux, Mac,
Windows).

The way that terminals (and emulators) handle colour is fundamentally
different from the DOS/Windows console. If you want something which will
work on both, you will have write separate implementations for terminals
and the DOS/Windows console.

For terminals, you can use the "curses" package, e.g.:

import curses

curses.setupterm()
setaf = curses.tigetstr('setaf')
setab = curses.tigetstr('setab')

def foreground(num):
if setaf:
sys.stdout.write(curses.tparm(setaf, num))

def background(num):
if setab:
sys.stdout.write(curses.tparm(setab, num))

For the Windows console, you'll need to use ctypes to interface to the
SetConsoleTextAttribute() function from Kernel32.dll.
 
A

Aahz

Anyone know of a way to print text in Python 3.1 with colors in a
portable way? In other words, I should be able to do something like
this:

print_color( "This is my text", COLOR_BLUE )

And this should be portable (i.e. it should work on Linux, Mac,
Windows).

Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
Python, and you can also use SDL via PyGame, wxWindows, pyQT, PyOpenGL,
and so on. If that's not an option, you should have said "command-line
program". ;-)
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz
 
R

r

On Aug 2, 9:52 pm, (e-mail address removed) (Aahz) wrote:
[snip]
Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
Python,
[snip]

Why is Tkinter such a whipping boy of the Python community? I know
it's very simple and does not have all the bells and whistles of wx,
but i think the gentle learning curve is very important for those
struggling to learn GUI's. Even today I always use Tkinter first, and
then only reach for wx when absolutely necessary. Seems to me the
relativity small footprint(GUI wise) compared to the payoffs are worth
the inclusion. I think if Tkinter where ever removed from Python it
would be not only a disservice to the language but also a detriment to
the *new* users of the language.

Just my humble opinion
 
J

Jean-Michel Pichavant

Nobody said:
The way that terminals (and emulators) handle colour is fundamentally
different from the DOS/Windows console. If you want something which will
work on both, you will have write separate implementations for terminals
and the DOS/Windows console.

For terminals, you can use the "curses" package, e.g.:

import curses

curses.setupterm()
setaf = curses.tigetstr('setaf')
setab = curses.tigetstr('setab')

def foreground(num):
if setaf:
sys.stdout.write(curses.tparm(setaf, num))

def background(num):
if setab:
sys.stdout.write(curses.tparm(setab, num))

For the Windows console, you'll need to use ctypes to interface to the
SetConsoleTextAttribute() function from Kernel32.dll.

FYI

http://github.com/jbowes/markymark/blob/59511b36a752b40243cc18fb0fb9800c74549ac1/markymark.py

If the URL ever becomes invalid, then google for markymark.py
You can use it either to color your Linux/Unix terms, or you can just
look at the python code to see how to set colors and attributes.


JM
 
A

Aahz

Why is Tkinter such a whipping boy of the Python community?

Can you explain what the connection is between my post and yours?
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"...string iteration isn't about treating strings as sequences of strings,
it's about treating strings as sequences of characters. The fact that
characters are also strings is the reason we have problems, but characters
are strings for other good reasons." --Aahz
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top