【问题标题】:Python Curses start_color() errorPython 诅咒 start_color() 错误
【发布时间】:2013-11-22 15:07:05
【问题描述】:

首先让我以我刚刚学习这个库并试图让它工作并在以后的迭代中清理它作为开头。

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

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(),而且我似乎在谷歌上找不到这个错误的任何例子,也找不到修复它的方法。

我已经尝试在任何地方添加它,但我很难过,谁能给我一些关于在哪里查看或示例的指导?

这是我的完整代码:

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()

【问题讨论】:

    标签: python colors curses


    【解决方案1】:

    在调用 initscr 后立即调用 start_color。

    IE:

    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 因为人们总是首先要问的是如何正确实现滚动,而这本书的大部分内容都涉及这个主题。

    【讨论】:

      【解决方案2】:

      在我的情况下(python 2.7),这段代码解决了我的问题:

      curses.start_color()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 2017-03-19
        • 1970-01-01
        • 2014-09-17
        相关资源
        最近更新 更多