使用 readchar 包读取 python 中的一个字符
Reading a character in python using readchar package
我正在尝试读取 python 中的一个字符,以便根据输入执行操作。我知道在 Google 的帮助下没有简单的方法来读取 python 中的击键。我终于找到了 'readchar' 包,它很有前途,但我无法让它在一个简单的程序中工作。非常感谢任何帮助。
import readchar
def keyprint():
while True:
print "Press 'A' to Start the recording"
print " 'Z' to Stop the recording"
print " 'Enter' to quit the program..."
# Read a key
key = readchar.readkey()
if(key == 'A'):
print "Started Recording..."
elif(key == 'Z'):
print "Stopped Recording..."
elif(key == '\r'):
print "Exiting..."
break
else:
print "Please Use only allowed keys: A, Z, Enter!"
if __name__ == "__main__":
keyprint()
编辑:输出错误
File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 22, in <module>
keyprint()
File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 10, in keyprint
key = readchar.readkey()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar.py", line 20, in readkey
c1 = getchar()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar_linux.py", line 12, in readchar
old_settings = termios.tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')
谢谢,
inblueswithu
在 github 上与项目创建者 magmax (here) 讨论了这个问题后,我了解到 readchar
包只有在您尝试 运行 它来自终端,但不是来自任何 IDE 或其他非终端执行。导致错误的原因是它试图获取在这种情况下不存在的终端设置。
我一直在尝试从 Wing IDE 运行 它。如果您尝试从终端 运行 该程序运行良好。
P.S:magmax 建议使用 readchar.keys.ENTER
而不是 \r
。并建议看看 https://github.com/magmax/python-inquirer & its examples
我正在尝试读取 python 中的一个字符,以便根据输入执行操作。我知道在 Google 的帮助下没有简单的方法来读取 python 中的击键。我终于找到了 'readchar' 包,它很有前途,但我无法让它在一个简单的程序中工作。非常感谢任何帮助。
import readchar
def keyprint():
while True:
print "Press 'A' to Start the recording"
print " 'Z' to Stop the recording"
print " 'Enter' to quit the program..."
# Read a key
key = readchar.readkey()
if(key == 'A'):
print "Started Recording..."
elif(key == 'Z'):
print "Stopped Recording..."
elif(key == '\r'):
print "Exiting..."
break
else:
print "Please Use only allowed keys: A, Z, Enter!"
if __name__ == "__main__":
keyprint()
编辑:输出错误
File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 22, in <module>
keyprint()
File "/home/inblueswithu/Documents/LM_DataCollection/keystroke_test.py", line 10, in keyprint
key = readchar.readkey()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar.py", line 20, in readkey
c1 = getchar()
File "/home/inblueswithu/.local/lib/python2.7/site-packages/readchar/readchar_linux.py", line 12, in readchar
old_settings = termios.tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')
谢谢, inblueswithu
在 github 上与项目创建者 magmax (here) 讨论了这个问题后,我了解到 readchar
包只有在您尝试 运行 它来自终端,但不是来自任何 IDE 或其他非终端执行。导致错误的原因是它试图获取在这种情况下不存在的终端设置。
我一直在尝试从 Wing IDE 运行 它。如果您尝试从终端 运行 该程序运行良好。
P.S:magmax 建议使用 readchar.keys.ENTER
而不是 \r
。并建议看看 https://github.com/magmax/python-inquirer & its examples