python curses.newwin不工作

时间:2010-07-03 05:22:53

标签: python windows window curses

我第一次学习curses,我决定在python中学习它,因为它比不断重新编译更容易。但是,我遇到了麻烦。当我尝试更新一个秒窗口时,我没有输出。这是一段代码:


import curses
win = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
field = curses.newwin(1, 20, 1, 1)
field.addstr(0, 0, "Hello, world!", curses.A_REVERSE)
field.refresh()

使用initscr()初始化的普通win窗口有效,但字段窗口不显示。有什么帮助吗?

编辑:这是新修订的代码,但仍然无效。

import curses

ex = None

def main(stdscr):
    global ex
    try:
        curses.curs_set(0)
    except Exception, e:
        ex = e

    field = curses.newwin(25, 25, 6, 6)
    field.border()
    cont = True
    x, y = 0, 0

    while cont:
        stdscr.clear()
        field.clear()
        coords = "%d, %d" % (x, y)
        stdscr.addstr(5, 5, coords, curses.A_REVERSE)
        field.addstr(y+2, x+2, "@", curses.A_BOLD)
        chr = stdscr.getkey()
        if chr == 'h':
            if x > 0: x -= 1
        if chr == 'l':
            if x < 20: x += 1
        if chr == 'j':
            if y > 0: y -= 1
        if chr == 'k':
            if y < 20: y += 1
        if chr == 'q':
            cont = False
            stdscr.clear()
            field.clear()
        stdscr.noutrefresh()
        field.noutrefresh()
        curses.doupdate()

curses.wrapper(main)

if ex is not None:
    print 'got %s (%s)' % (type(ex).__name__, ex)

2 个答案:

答案 0 :(得分:3)

似乎对我好 - 我总是使用curses.wrapper而我的终端不支持0的光标可见性,所以这就是我的......:

import curses

ex = None

def main(stdscr):
    global ex
    try:
        curses.curs_set(0)
    except Exception, e:
        ex = e

    field = curses.newwin(1, 20, 1, 1)
    field.addstr(0, 0, "Hello, world!", curses.A_REVERSE) 
    field.refresh()
    field.getch()

curses.wrapper(main)
if ex is not None:
  print 'got %s (%s)' % (type(ex).__name__, ex)

我看到反向的“Hello,world!”,然后当我按任意键以满足getch时,程序终止了预期的消息got error (curs_set() returned ERR)

你看到这个节目有什么......? (请记住包装器执行initscr并设置noechocbreak - 更重要的是在完成后重置它,这就是我总是使用它的原因; - )。

BTW,我在Mac(OSx 10.5.8)和Terminal.App上使用Py 2.6.4。你的平台......?

答案 1 :(得分:2)

啊,发现了问题。当我使用stdscr.clear()时,它正在清除整个终端,包括新窗口。我需要做的就是制作两个窗口,每个窗口分别对应一个窗口。

哦,感谢上面的curses.wrapper提示。在这里说因为我不能发表评论。