pythoncurses.newwin 不工作

发布于 2024-09-08 09:15:40 字数 1385 浏览 2 评论 0原文

我是第一次学习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)

I'm learning curses for the first time, and I decided to do it in python because it would be easier than constantly recompiling. However, I've hit a hitch. When I try to update a seccond window, I get no output. Here's a code snippet:


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

The normal win window initialized with initscr() works, but the field window doesn't show up. Any help?

Edit: Here's the new, revised code, which still doesn't work.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-09-15 09:15:40

对我来说似乎没问题——我总是使用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 ——更重要的是在完成后重置它,这就是我总是使用它的原因; -)。

顺便说一句,我在 Mac (OSx 10.5.8) 和 Terminal.App 上使用 Py 2.6.4。你的平台...?

Seems OK to me -- I always use curses.wrapper and my terminal doesn't support cursor visibility of 0, so this is what I have...:

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)

I see the reversed "Hello, world!", then when I hit any key to satisfy the getch the program terminates with the expected msg got error (curs_set() returned ERR).

What are you seeing w/this program...? (Remember the wrapper does initscr and sets noecho and cbreak -- and more importantly resets it when done, which is why I always use it;-).

BTW, I'm using Py 2.6.4 on a Mac (OSx 10.5.8) and Terminal.App. Your platform...?

风和你 2024-09-15 09:15:40

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

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

Ah, found the problem. When I use stdscr.clear(), it's clearing the entire terminal, including the new window. All I needed to do is make two windows, one for each separate display.

Oh, and thanks to above for curses.wrapper tip. Saying here because I can't post a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文