Curses print characters as typed

徘徊边缘 提交于 2021-02-11 17:41:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!