如何使用curses和Python设置窗口背景颜色?

时间:2019-05-16 19:17:28

标签: python colors curses

我的目标:使窗口背景具有特定的颜色。

我当前的代码:

import curses


def do_it(win):  # Shia LeBeouf!
    win.bkgd(' ', curses.COLOR_BLUE)
    win.addstr(1,1, "This is not blue")
    win.getch()

if __name__ == '__main__':
    curses.wrapper(do_it)

我希望我的窗口是蓝色,并出现“这不是蓝色”。相反,我得到了这个窗口:

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$This$is$not$blue$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

它甚至不是很蓝。

我也尝试过:

  • win.bkgd(curses.COLOR_BLUE)-似乎删除了所有空格(?)
  • win.bkgdset(' ', curses.COLOR_BLUE)-似乎做同样的事情

这些,还有更多,无济于事。

然后问题仍然存在:如何在curses中设置窗口的背景颜色?

1 个答案:

答案 0 :(得分:1)

显然,您必须先使用curses.init_pair来指定颜色,然后再使用它们。然后,您可以将它们与curses.color_pair一起使用:

import curses


def do_it(win):  # Shia LeBeouf!
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
    win.bkgd(' ', curses.color_pair(1) | curses.A_BOLD)
    win.addstr(1,1, "This is not blue")
    win.getch()
    win.bkgd(' ', curses.color_pair(1) | curses.A_BOLD | curses.A_REVERSE)
    win.addstr(1,1, "This is now blue")
    win.getch()

if __name__ == '__main__':
    curses.wrapper(do_it)