我试图读取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()
编辑:输出错误
^{pr2}$谢谢, 布鲁斯威图
在github上与项目的创建者magmax(here)讨论了这个问题之后,我明白了
readchar
包只有在尝试从终端运行时才有效,而不是从任何IDE或其他非终端执行中运行。错误是因为它试图获取在这种情况下不存在的终端设置。在我一直在试着从侧翼控制它。如果你能从终端上运行一个很好的程序。在
注:magmax建议使用}。建议你看看https://github.com/magmax/python-inquirer&;its examples
readchar.keys.ENTER
,而不是{相关问题 更多 >
编程相关推荐