我有一个脚本应该与用户输入交互(按箭头键),但我无法获取按键。我尝试了 raw_input 和其他一些函数,但它们不起作用。这是我的示例代码,它应该是什么样子(可以在另一个函数中将运行 bool 设置为 False)
running = True
while running:
#if input == Arrow_UP:
# do_Sth
#elif ...
display()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
另一个问题是,如何每秒只调用一次显示函数,但立即对输入做出反应?
我想知道在python中绑定键的最简单方法
例如,默认的python控制台窗口会先提示并等待,然后在psuedo->
if key "Y" is pressed:
print ("Yes")
if key "N" is pressed:
print ("No")
Run Code Online (Sandbox Code Playgroud)
我想在不使用python不包含的任何模块的情况下实现这一目标。只是纯蟒蛇
任何帮助都将不胜感激
python 2.7或3.x Windows 7
注意: raw_input()要求用户按Enter键,因此不进行键盘绑定
def confirm_choice():
confirm = input("[c]Confirm or [v]Void: ")
if confirm != 'c' and confirm != 'v':
print("\n Invalid Option. Please Enter a Valid Option.")
confirm_choice()
print (confirm)
return confirm
Run Code Online (Sandbox Code Playgroud)
例如,当键入无效输入时,字母“k”后跟有效输入“c”,该函数将打印输入“c”和“k”
输出:
c
k
Run Code Online (Sandbox Code Playgroud)
如何更改上述程序,使其仅返回“c”或“v”,并在输入无效时重复该函数。
我试图getch通过pip 安装,我有一个clang错误:
python -m pip install getch
Collecting getch
Using cached getch-1.0.tar.gz
Installing collected packages: getch
Running setup.py install for getch ... error
Complete output from command /usr/local/opt/python/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/qm/gj1n93fd7rg8rgz1ldq19gm80000gn/T/pip-build-RYKX8n/getch/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/qm/gj1n93fd7rg8rgz1ldq19gm80000gn/T/pip-uEGRgA-record/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_ext
building 'getch' extension
creating build
creating build/temp.macosx-10.10-x86_64-2.7
clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/include -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c getchmodule.c -o build/temp.macosx-10.10-x86_64-2.7/getchmodule.o …Run Code Online (Sandbox Code Playgroud) 我想这样做,以便我接受输入
enterKey = raw_input("String here: ")
Run Code Online (Sandbox Code Playgroud)
但是输入的任何键都没有显示 - 它应该只是在脚本中作为一种暂停,直到按下Enter/Return然后它就可以继续.
在Python中,什么是最简单和最Pythonic的方式来暂停用户按键将重新启动循环的循环?我正在考虑将此作为调试辅助工具,以便我可以从循环内向stdout输出一些调试消息,而不会立即丢弃大量文本.
可能重复:
Python从用户读取单个字符
我使用下面的代码.但不是接受单个字符,而是允许用户放置多个字符.
我该如何解决这个问题?
guess = raw_input(':')
guessInLower = guess.lower()
Run Code Online (Sandbox Code Playgroud) 如何限制用户可以输入的字符数量raw_input?我的问题可能有一些简单的解决方案,但我无法弄清楚.
如果我使用 raw_input() 它需要所有用户输入。我想在用户输入“-1”时停止输入。
我的意思是如果用户输入'12 22 -23 3 -1 23 -1 23',它不应该在3之后读取。
任何其他读取输入的方式也可以。
我找到了各种方法来检测任何按键,从单击上的诅咒到创建一个函数来执行此操作(还有 msvcrt,但这有时必须在 Linux 上工作),但我总是遇到同样的问题:无论我按下哪个箭头键,所有这些函数返回了b'\xe0'。我在cmd和powershell中尝试过,结果相同。我运行的是Win7 Pro 64位。
编辑:抱歉,我使用了这段代码并尝试msvcrt.getch()了click.getchar()
python ×9
python-2.7 ×3
python-3.x ×3
function ×1
getch ×1
keyboard ×1
pip ×1
raw-input ×1
stdin ×1
windows ×1