首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Curses start_color()错误

Python Curses start_color()错误
EN

Stack Overflow用户
提问于 2013-11-11 06:00:57
回答 2查看 1.9K关注 0票数 2

首先,让我开始学习这个库,我正在尝试让它工作,并将在以后的迭代中清理它。

也就是说,我的代码抛出了这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "cursesDemo1.py", line 30, in <module>
    box3 = maketextbox(1,40, 10,20,"foo",deco="underline",textColorpair=curses.color_pair (0),decoColorpair=curses.color_pair(1))
_curses.error: must call start_color() first

我不确定我需要在哪里调用start_color(),我似乎在谷歌上找不到任何这个错误的例子,也找不到修复它的方法。

我试着到处添加它,但被难住了,谁能给我一些指导,看看哪里或一个例子?

下面是我的完整代码:

代码语言:javascript
复制
import curses 
import time

screen = curses.initscr()

def maketextbox(h,w,y,x,value="",deco=None,underlineChr=curses.ACS_HLINE,textColorpair=0,decoColorpair=0):
    nw = curses.newwin(h,w,y,x)
    txtbox = curses.textpad.Textbox(nw)
    if deco=="frame":
        screen.attron(decoColorpair)
        curses.textpad.rectangle(screen,y-1,x-1,y+h,x+w)
        screen.attroff(decoColorpair)
    elif deco=="underline":
         screen.hline(y+1,x,underlineChr,w,decoColorpair)

    nw.addstr(0,0,value,textColorpair)
    nw.attron(textColorpair)
    screen.refresh()
    return txtbox

 try:
    screen.border(0)

    box1 = curses.newwin(22, 50, 3, 5)
    box1.box()   

    box2 = curses.newwin(22, 50, 3, 65)
    box2.box()   

    box3 = maketextbox(1,40,  10,20,"foo",deco="underline",textColorpair=curses.color_pair    (0),decoColorpair=curses.color_pair(1))
    textInput = box3.edit()

    box1.addstr(2, 18, "Functions")
    box2.addstr(2, 18, "Processes")

    screen.refresh()
    box1.refresh()
    box2.refresh()
    box3.refresh()

    for i in range(19):
        toWrite = "Does this move run = %d" % i
        box1.addstr(8, 9, toWrite)
        box1.refresh()
        time.sleep(5)
        box2.addstr(8, 9, textInput)
    screen.getch()



finally:
    curses.endwin()
EN

回答 2

Stack Overflow用户

发布于 2013-12-02 19:09:14

在调用initscr之后立即调用start_color。

即:

代码语言:javascript
复制
if __name__ == "__main__":
    screen = curses.initscr()
    screen.start_color()
    ...
    screen.endwin()

Eric S. Raymond的“用ncurses编写程序”是对该库的低级屏幕管理部分的很好的一般性介绍。你不需要了解C语言就能理解它,因为库函数很大程度上以1:1的方式直接映射到它们的Python对应物:

http://invisible-island.net/ncurses/ncurses-intro.html

还有:http://tinyurl.com/lgkyggq,因为人们问的第一件事总是如何正确地实现滚动,这本书的很大一部分都涉及到了这个主题。

票数 0
EN

Stack Overflow用户

发布于 2018-04-22 01:49:00

在我的例子(python 2.7)中,这段代码修复了我的问题:

代码语言:javascript
复制
curses.start_color()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19895768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档