Printing with colors in a portable way

Discussion in 'Python' started by Robert Dailey, Jul 30, 2009.

  1. 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).
     
    Robert Dailey, Jul 30, 2009
    #1
    1. Advertising

  2. Robert Dailey

    Rhodri James Guest

    On Thu, 30 Jul 2009 23:40:37 +0100, Robert Dailey <>
    wrote:

    > 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.

    --
    Rhodri James *-* Wildebeest Herder to the Masses
     
    Rhodri James, Jul 31, 2009
    #2
    1. Advertising

  3. Robert Dailey

    Nobody Guest

    On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote:

    > 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.
     
    Nobody, Jul 31, 2009
    #3
  4. Robert Dailey

    Aahz Guest

    In article <>,
    Robert Dailey <> wrote:
    >
    >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 () <*> 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
     
    Aahz, Aug 3, 2009
    #4
  5. Robert Dailey

    r Guest

    On Aug 2, 9:52 pm, (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
     
    r, Aug 3, 2009
    #5
  6. Nobody wrote:
    > On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote:
    >
    >
    >> 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.
    >
    >


    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
     
    Jean-Michel Pichavant, Aug 3, 2009
    #6
  7. Robert Dailey

    Aahz Guest

    In article <>,
    r <> wrote:
    >On Aug 2, 9:52=A0pm, (Aahz) wrote:
    >
    >> Much as I hate to say, use a cross-platform GUI -- Tkinter comes with
    >> Python,

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


    Can you explain what the connection is between my post and yours?
    --
    Aahz () <*> 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
     
    Aahz, Aug 5, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eli Bendersky
    Replies:
    1
    Views:
    1,206
    Mike Treseler
    Mar 1, 2006
  2. John Oliver

    Colors not printing

    John Oliver, Jan 12, 2005, in forum: HTML
    Replies:
    9
    Views:
    596
    Jeffrey Silverman
    Jan 13, 2005
  3. Replies:
    7
    Views:
    947
  4. AAaron123

    TreeNode colors come from anchor colors

    AAaron123, Aug 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    562
    darrel
    Aug 7, 2008
  5. ZelluX
    Replies:
    3
    Views:
    1,960
    Peter Otten
    Dec 1, 2008
Loading...

Share This Page