简体   繁体   English

使用 Python 诅咒颜色,我不明白我的代码有什么问题

[英]Using Python curses colors, I don't understand what is wrong with my code

I want to use Python curses library to make the fisrt draft to a new app.我想使用 Python 诅咒库来制作新应用程序的第一份草稿。 I have the basics of curses, but don't know why the example below writes the text in gray instead of red :我有诅咒的基础知识,但不知道为什么下面的示例将文本写成灰色而不是红色:

import curses
from curses import wrapper
from time import sleep

def main(stdscr):
    curses.noecho()
    curses.cbreak()
    curses.start_color()
    stdscr.keypad(True)
    stdscr.addstr(10,10,"This text should be red",curses.COLOR_RED)
    stdscr.refresh()
    sleep(2)
    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    curses.endwin()

wrapper(main)

I know I shouldn't rewrite the initialization steps after using "wrapper", but that's not the issue, I think, the text is still gray without it.我知道我不应该在使用“包装器”后重写初始化步骤,但这不是问题,我认为,没有它,文本仍然是灰色的。 I run this app in an xterm window, "has_colors" tells me the term is color capable (which I know because "ls" is colorfull).我在 xterm 窗口中运行此应用程序,“has_colors”告诉我该术语具有颜色功能(我知道这是因为“ls”是彩色的)。

If anyone could explain to me what I am doing wrong, I would be delighted :-P如果有人能向我解释我做错了什么,我会很高兴:-P

Thanks for any help.谢谢你的帮助。

The attribute should use a color pair , not a color number .该属性应该使用颜色对,而不是颜色编号 You would make a color pair using init_pair , use it viacolor_pair您将使用init_pair制作颜色对,通过color_pair使用它

For example例如

import curses
from curses import wrapper
from time import sleep

def main(stdscr):
    curses.noecho()
    curses.cbreak()
    curses.start_color()
    stdscr.keypad(True)
    curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLUE)
    stdscr.addstr(10,10,"This text should be red",curses.color_pair(1))
    stdscr.refresh()
    sleep(2)
    curses.nocbreak()
    stdscr.keypad(False)
    curses.echo()
    curses.endwin()

wrapper(main)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM