诅咒键入的打印字符 [英] Curses print characters as typed
问题描述
使用python我想打印出我输入的字符,这在java脚本中很容易,但是我不理解如何使用curses模块,这是我尝试过的代码,但是没有用.
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()
请您解释一下我如何使用curses模块的这一部分.
could you please explain how i use this part of the curses module.
推荐答案
如果您只想获取用户输入并在键入字符时将其打印出来,则没有什么可做的:
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'))
在这里,您只需调用curses.initscr()来初始化curses模块.它为您提供了stdScr,而您只需要调用stdScr的getstr()方法(实际上是一个curses.window对象),它将为您提供用户输入,以便在键入字符时打印它们.
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.
我不知道您是否已经签出,但是很清楚: http://docs.python.org/3.3/library/curses.html
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 !)
这篇关于诅咒键入的打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!