windows python curses.newwin 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3170406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python curses.newwin not working
提问by Hussain
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:
我是第一次学习 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()
The normal win window initialized with initscr() works, but the field window doesn't show up. Any help?
用 initscr() 初始化的普通 win 窗口可以工作,但字段窗口不显示。有什么帮助吗?
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)
采纳答案by Hussain
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.
啊,发现问题了。当我使用 stdscr.clear() 时,它会清除整个终端,包括新窗口。我需要做的就是制作两个窗口,每个单独的显示器一个。
Oh, and thanks to above for curses.wrapper tip. Saying here because I can't post a comment.
哦,感谢上面的 curses.wrapper 提示。在这里说是因为我无法发表评论。
回答by Alex Martelli
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...:
对我来说似乎没问题——我总是使用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)
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)
.
我看到相反的“你好,世界!”,然后当我按任意键来满足getch
程序以预期的 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;-).
你在这个程序中看到了什么......?(请记住包装器会执行initscr
并设置noecho
和cbreak
- 更重要的是在完成后重置它,这就是我总是使用它的原因;-)。
BTW, I'm using Py 2.6.4 on a Mac (OSx 10.5.8) and Terminal.App. Your platform...?
顺便说一句,我在 Mac (OSx 10.5.8) 和 Terminal.App 上使用 Py 2.6.4。你的平台……?