python 如何制作不需要用户按 [enter] 进行选择的菜单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:23:31  来源:igfitidea点击:

How do I make a menu that does not require the user to press [enter] to make a selection?

python

提问by Grant

I've got a menu in Python. That part was easy. I'm using raw_input()to get the selection from the user.

我有一个 Python 菜单。那部分很容易。我正在使用raw_input()从用户那里获取选择。

The problem is that raw_input(and input) require the user to press Enterafter they make a selection. Is there any way to make the program act immediately upon a keystroke? Here's what I've got so far:

问题是raw_input(和输入)要求用户Enter在做出选择后按下。有没有办法让程序在击键时立即起作用?这是我到目前为止所得到的:

import sys
print """Menu
1) Say Foo
2) Say Bar"""
answer = raw_input("Make a selection> ")

if "1" in answer: print "foo"
elif "2" in answer: print "bar"

It would be great to have something like

有类似的东西会很棒

print menu
while lastKey = "":
    lastKey = check_for_recent_keystrokes()
if "1" in lastKey: #do stuff...

采纳答案by Mark Harrison

On Windows:

在 Windows 上:

import msvcrt
answer=msvcrt.getch()

回答by Mark Harrison

On Linux:

在 Linux 上:

  • set raw mode
  • select and read the keystroke
  • restore normal settings
  • 设置原始模式
  • 选择并读取击键
  • 恢复正常设置
import sys
import select
import termios
import tty

def getkey():
    old_settings = termios.tcgetattr(sys.stdin)
    tty.setraw(sys.stdin.fileno())
    select.select([sys.stdin], [], [], 0)
    answer = sys.stdin.read(1)
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
    return answer

print """Menu
1) Say Foo
2) Say Bar"""

answer=getkey()

if "1" in answer: print "foo"
elif "2" in answer: print "bar"

回答by Grant

Wow, that took forever. Ok, here's what I've ended up with

哇,这花了很长时间。好的,这就是我的结果

#!C:\python25\python.exe
import msvcrt
print """Menu
1) Say Foo 
2) Say Bar"""
while 1:
    char = msvcrt.getch()
    if char == chr(27): #escape
        break
    if char == "1":
        print "foo"
        break
    if char == "2":
        print "Bar"
        break

It fails hard using IDLE, the python...thing...that comes with python. But once I tried it in DOS (er, CMD.exe), as a real program, then it ran fine.

使用 IDLE 很难失败,python ……东西……python 附带的东西。但是一旦我在 DOS(呃,CMD.exe)中作为一个真正的程序尝试过它,它就运行良好。

No one try it in IDLE, unless you have Task Manager handy.

除非您手边有任务管理器,否则没人会在 IDLE 中尝试它。

I've already forgotten how I lived with menus that arn't super-instant responsive.

我已经忘记了我是如何使用不是超级即时响应的菜单的。

回答by helloandre

The reason msvcrt fails in IDLE is because IDLE is not accessing the library that runs msvcrt. Whereas when you run the program natively in cmd.exe it works nicely. For the same reason that your program blows up on Mac and Linux terminals.

msvcrt 在 IDLE 中失败的原因是因为 IDLE 没有访问运行 msvcrt 的库。而当您在 cmd.exe 中本地运行该程序时,它运行良好。出于同样的原因,您的程序会在 Mac 和 Linux 终端上崩溃。

But I guess if you're going to be using this specifically for windows, more power to ya.

但我想如果你打算专门为 Windows 使用它,那么你会更有力量。