首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >window.getch()不返回与main_screen.getch()相同的代码

window.getch()不返回与main_screen.getch()相同的代码
EN

Stack Overflow用户
提问于 2022-07-27 23:16:02
回答 1查看 36关注 0票数 0

我对ncurses (linux)有编码问题。我试图在Python 例7中复制C中的ncurses编程方法,我确实成功地运行了这个示例。

代码语言:javascript
复制
import curses

@curses.wrapper
def main(main_screen):
    w, h = 5, 3
    x, y = curses.COLS // 2, curses.LINES // 2

    def mkwin(w, h, x, y):
        win = curses.newwin(h, w, y, x)
        win.box(0, 0)
        win.refresh()
        return win

    def clearwin(win):
        win.border(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ')
        win.refresh()

    main_screen.addstr("Press Q to quit.")
    main_screen.refresh()
    testwin = mkwin(w, h, x, y)

    while True:
        key = main_screen.getch()
        if key in {curses.KEY_BACKSPACE, ord('q')}:
            break
        if key == curses.KEY_LEFT:
            clearwin(testwin)
            del testwin
            x -= 1
            testwin = mkwin(w, h, x, y)
        if key == curses.KEY_RIGHT:
            clearwin(testwin)
            del testwin
            x += 1
            testwin = mkwin(w, h, x, y)
        if key == curses.KEY_UP:
            clearwin(testwin)
            del testwin
            y -= 1
            testwin = mkwin(w, h, x, y)
        if key == curses.KEY_DOWN:
            clearwin(testwin)
            del testwin
            y += 1
            testwin = mkwin(w, h, x, y)

但是,我想删除包含该"Press Q to quit."消息的第一个屏幕。所以我改变了

代码语言:javascript
复制
    main_screen.addstr("Press Q to quit.")
    main_screen.refresh()
    testwin = mkwin(w, h, x, y)

代码语言:javascript
复制
    testwin = mkwin(w, h, x, y)

但相反,我有一个空的第一个屏幕,将停留直到我按下一个键。显然,getch()函数的main_screen导致整个屏幕清除,所以我必须使用getch()testwin

但是,testwin.getch()方法不返回单个键按下的很好的整数代码:相反,它返回微调键代码。例如,它返回27,然后返回91,而不是单个259 main_screen.getch()返回。如何配置testwin以使getch返回testwinmain_screen的值?

注意:我尝试使用main_screen.subwin而不是curses.newwin,但是它没有改变任何东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-28 07:55:15

python诅咒“包装”调用keypad方法,这不是newwin的默认方法。源代码会这样做:

代码语言:javascript
复制
    # In keypad mode, escape sequences for special keys
    # (like the cursor keys) will be interpreted and
    # a special value like curses.KEY_LEFT will be returned
    stdscr.keypad(1)

如果添加win.keypad(1),它将解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73145558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档