[Python在按下终端上的箭头键时输入输入时打印转义键

问题描述 投票:0回答:1

我制作了一个简单的Python 3脚本,该脚本接受了用户的输入。但是在输入时,如果我按向左箭头键,而不是向左打印^ [[D。使用所有箭头键都会发生这种情况。但是它不会在Terminal或Python Interactive Shell中发生,仅当我从Terminal运行Python脚本并需要输入输入时才会发生。

我使用运行Python 3.7的Ubuntu 19.10和Anaconda distrubition。

operation = input("Enter the expression: ")

我该如何解决?

python shell terminal anaconda arrow-keys
1个回答
0
投票

使用readline之前导入input程序包

import readline
operation = input("Enter the expression: ")

https://docs.python.org/3/library/readline.html

使用此模块进行的设置会影响口译员的互动提示以及内置input()函数。

导入就足以激活输入行编辑。 readline模块中的其他功能可用于设置选项卡补全和历史记录文件。

© www.soinside.com 2019 - 2024. All rights reserved.