Hogofwar
Hogofwar

Reputation: 51

Not able to change colours in curses on windows

I am using curses on windows from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses using 32 bit build for Python 3.4

It's been working really well so far except for the colours. It reports that it can change colours, but when trying to actually change the colours, nothing happens. Is this a limitation of curses for windows or a problem with my code?

import curses

def main(stdscr):
    curses.start_color()
    stdscr.addstr("Can Change Color? %s\n" % str(curses.can_change_color()))
    for i in range(0, curses.COLORS):
        curses.init_color(i, 1000, 0, 0)
        curses.init_pair(i + 1, i, 0)
    try:
        for i in range(0, 255):
            stdscr.addstr(str(i), curses.color_pair(i))
    except curses.ERR:
        pass
    stdscr.getch()

curses.wrapper(main)

In this, it reports that it can change colours, but when trying to set every colour to red (as a test), they all stay default.

Upvotes: 2

Views: 1882

Answers (1)

Prahlad Yeri
Prahlad Yeri

Reputation: 3653

I know its been quite a while since you asked this question, but since this is one of the first hits for a google search like "python curses color issues", I thought I may as well answer this.

This linked answer contains the exact solution you are looking for. You just don't call curses.color_pair(i) directly, but you have to define those pairs first:

stdscr = curses.initscr()
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
stdscr.addstr( "Pretty text", curses.color_pair(1) )
stdscr.refresh()

The above code will display the word "Pretty text" in red color and white background (but make sure that the console supports coloring by calling curses.has_colors() first.

Upvotes: 4

Related Questions