python curses.newwin不工作

Hus*_*ain 5 python windows curses window

我是第一次学习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()
Run Code Online (Sandbox Code Playgroud)

使用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)
Run Code Online (Sandbox Code Playgroud)

Hus*_*ain 3

啊,发现问题了。当我使用 stdscr.clear() 时,它会清除整个终端,包括新窗口。我所需要做的就是创建两个窗口,一个用于每个单独的显示器。

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