更新时间:2023-11-25 17:23:04
这适用于Ubuntu 8.04.1,Python 2.5.2,但我没有得到这样的错误.可能是您应该从命令行尝试它,eclipse可能正在使用它自己的stdin,如果从Wing IDE运行它,我会得到完全相同的错误,但是从命令行来看,它的效果很好. 原因是IDE(例如Wing)正在使用自己的类netserver.CDbgInputStream作为sys.stdin 所以sys.stdin.fileno为零,这就是为什么会出错. 基本上,IDE stdin不是tty(print sys.stdin.isatty()为False)
This is working on Ubuntu 8.04.1 , Python 2.5.2, i get no such error. May be you should try it from command line, eclipse may be using its own stdin, i get exact same error if I run it from Wing IDE, but from command line it works great. Reason is that IDE e.g Wing is using there own class netserver.CDbgInputStream as sys.stdin so sys.stdin.fileno is zero, thats why the error. Basically IDE stdin is not a tty (print sys.stdin.isatty() is False)
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
getch = _GetchUnix()
print getch()