pythoncurses.newwin 不工作
我是第一次学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说似乎没问题——我总是使用curses.wrapper,并且我的终端不支持光标可见性0,所以这就是我所拥有的......:
我看到相反的“Hello,world! ”,然后当我按任意键来满足
getch
时,程序将终止并显示预期的消息got error (curs_set() returned ERR)
。您在这个程序中看到了什么...? (请记住,包装器执行
initscr
并设置noecho
和cbreak
——更重要的是在完成后重置它,这就是我总是使用它的原因; -)。顺便说一句,我在 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...:I see the reversed "Hello, world!", then when I hit any key to satisfy the
getch
the program terminates with the expected msggot error (curs_set() returned ERR)
.What are you seeing w/this program...? (Remember the wrapper does
initscr
and setsnoecho
andcbreak
-- 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...?
啊,发现问题了。当我使用 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.