问题
Using python I am wanting to have the characters printed out as i type, this is easy in java script, but I am not understanding how to use the curses module, this is the code that I tried but it did not work.
import curses
stdscr = curses.initscr()
curses.echo()
curses.cbreak()
a = raw_input()
print a
stdscr.refresh()
could you please explain how i use this part of the curses module.
回答1:
If you just want to get the user input and make the characters printed out as they are typed, there's not much to do :
import curses
stdScr = curses.initscr()
myInput = stdScr.getstr()
curses.endwin()
print(myInput.decode('utf-8'))
Here, you just initialize the curses module by calling curses.initscr(). It gives you the stdScr and you just have to call the getstr() method of stdScr (which is actually a curses.window object) which will give you the user input printing the characters as they are typed.
I don't know if you've already checked this out, but it's very clear : http://docs.python.org/3.3/library/curses.html
Have fun ! (curses is an awesome module !)
来源:https://stackoverflow.com/questions/17458087/curses-print-characters-as-typed