使用readchar packag读取python中的字符

2024-09-27 07:31:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图读取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}$

谢谢, 布鲁斯威图


Tags: thetokeyifgoogle字符关键print
1条回答
网友
1楼 · 发布于 2024-09-27 07:31:36

在github上与项目的创建者magmax(here)讨论了这个问题之后,我明白了readchar包只有在尝试从终端运行时才有效,而不是从任何IDE或其他非终端执行中运行。错误是因为它试图获取在这种情况下不存在的终端设置。在

我一直在试着从侧翼控制它。如果你能从终端上运行一个很好的程序。在

注:magmax建议使用readchar.keys.ENTER,而不是{}。建议你看看https://github.com/magmax/python-inquirer&;its examples

相关问题 更多 >

    热门问题