函数raw_input( )在curses模式下不起作用,getch()方法返回一个整数;它表示按下的键的 ASCII 码。如果您想从提示符中扫描字符串,这将不起作用。你可以使用getstr函数:
从用户那里读取一个字符串,具有原始的行编辑能力。
还有一个检索整个字符串的方法,getstr()
curses.echo() # Enable echoing of characters
# Get a 15-character string, with the cursor on the top line
s = stdscr.getstr(0,0, 15)
我写了raw_input函数如下:
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_input(stdscr, 5, 5, "cool or hot?")
编辑:这是工作示例:
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()
输出: