在 Windows 中使用 python3 是不可能在不按回车键的情况下从屏幕输入单个字符的吗? [英] Is it still true that with python3 in Windows it is impossible to input a single character from the screen without hitting enter?
问题描述
unix下可以用termios,windows下不行.
It can be done with termios under Unix, but not under Windows.
我可以在 perl 中使用 TERM::Readkey 或在 Windows 批处理文件中轻松完成.
I do it easily in perl with TERM::Readkey, or in Windows Batch files.
如果这里给出了答案,我一直找不到.
If an answer has been given here, I have been unable to find it.
我觉得很难相信 Python 会忽略所有其他语言都可以使用的基本操作.
I find it hard to believe that python omits such a basic operation available in essentially all other languages.
推荐答案
上面的回答返回的是一个字节,不是字符,所以需要解码.
The answer above returns a byte, not a character, so it needs to be decoded.
这是一种无需按 Enter 即可返回单个字符的解决方案.它适用于 Windows 上的 python 3.4
Here is a solution that returns a single character without having to hit Enter. It works in python 3.4, on Windows
def getChar():
import msvcrt
return msvcrt.getch().decode('utf-8')
这篇关于在 Windows 中使用 python3 是不可能在不按回车键的情况下从屏幕输入单个字符的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!