The raw_input( )
function does not work in curses mode, the getch()
method returns an integer; it is the ASCII code of the pressed key. Will not work if you want to scan a line from the prompt. You can use the getstr
function:
Read a line from the user with the ability to edit a primitive line.
There is also a method to extract the entire string, getstr()
curses.echo()
And I wrote a raw_input function as shown below:
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
name it choice = my_raw_input(stdscr, 5, 5, "cool or hot?")
Edit: Here is a working example:
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()
output:
![enter image description here](https://fooobar.com/undefined)
source share