Python 如何在ncurses屏幕中输入单词?

Python 如何在ncurses屏幕中输入单词?,python,ncurses,Python,Ncurses,我试图先使用raw\u input()函数,但发现它与ncurses兼容。 然后我尝试了window.getch()函数,我可以在屏幕上输入和显示字符,但无法实现输入。如何在ncurses中输入单词,并使用if语句对其求值 例如,我想在ncurses中实现这一点: import ncurses stdscr = curses.initscr() # ???_input = "cool" # this is the missing input method I want to know if ?

我试图先使用
raw\u input()
函数,但发现它与ncurses兼容。
然后我尝试了
window.getch()
函数,我可以在屏幕上输入和显示字符,但无法实现输入。如何在
ncurses
中输入单词,并使用if语句对其求值

例如,我想在
ncurses
中实现这一点:

import ncurses
stdscr = curses.initscr()

# ???_input = "cool" # this is the missing input method I want to know
if ???_input == "cool":
    stdscr.addstr(1,1,"Super cool!")
stdscr.refresh()
stdscr.getch()
curses.endwin()
函数
raw\u input()
在curses模式下不工作,
getch()
方法返回一个整数;它表示按下的键的ASCII码。如果要从提示符扫描字符串,则该命令将不起作用。您可以使用
getstr
函数:

从用户处读取字符串,并具有基本行编辑功能

还有一种方法可以检索整个字符串,
getstr()

我编写了原始输入函数,如下所示:

def my_raw_input(stdscr, r, c, prompt_string):
    curses.echo() 
    stdscr.addstr(r, c, prompt_string)
    stdscr.refresh()
    input = stdscr.getstr(r + 1, c, 20)
    return input  #       ^^^^  reading input at next line  
称之为
choice=my_raw_输入(stdscr,5,5,“冷还是热?”)

编辑:以下是工作示例:

if __name__ == "__main__":
    stdscr = curses.initscr()
    stdscr.clear()
    choice = my_raw_input(stdscr, 2, 3, "cool or hot?").lower()
    if choice == "cool":
        stdscr.addstr(5,3,"Super cool!")
    elif choice == "hot":
        stdscr.addstr(5, 3," HOT!") 
    else:
        stdscr.addstr(5, 3," Invalid input") 
    stdscr.refresh()
    stdscr.getch()
    curses.endwin()
输出:

两个问题。在“input=stdscr.getstr(r+1,c,20)”中,我为什么要从r+1读取(因为curosr会自动移动到那里?)r+1和c之后的“20”是什么意思?@Mario(1)我做了
r+1
将光标移动到下一行(因为Couser会在
之后自动移动到下一行),“20”意味着可以输入20个字符的字符串。阅读我在回答中添加的所有评论和文档
if __name__ == "__main__":
    stdscr = curses.initscr()
    stdscr.clear()
    choice = my_raw_input(stdscr, 2, 3, "cool or hot?").lower()
    if choice == "cool":
        stdscr.addstr(5,3,"Super cool!")
    elif choice == "hot":
        stdscr.addstr(5, 3," HOT!") 
    else:
        stdscr.addstr(5, 3," Invalid input") 
    stdscr.refresh()
    stdscr.getch()
    curses.endwin()