Pydev/Eclipse 中的单字符输入

编程入门 行业动态 更新时间:2024-10-27 13:31:50
本文介绍了Pydev/Eclipse 中的单字符输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在没有输入"的情况下捕获单个用户按键,然后查看它是r"还是b"等,但特别是在带有 PyDev 的 Eclipse 中(Windows 7:64 位,Python 3.6.1).Python 从用户读取单个字符 当然.mrvcrt 似乎在 cmd.exe 中有效,但在 PyDev 中无效:

I want to capture a single user keypress without "enter", and see afterward whether it was 'r' or 'b' etc, but specifically in Eclipse with PyDev (Windows 7: 64bit, Python 3.6.1). Many nice alternatives are mentioned in Python read a single character from the user of course. The mrvcrt seems to work in cmd.exe but not in PyDev:

import msvcrt mych = msvcrt.getwch() print('You pressed: ' + mych)

为什么不呢?我看到@MatthieuRiegler 已经在 在 Eclipse/PyDev 中使用 msvcrt.getch() 提出了这个问题 ...但我对任何有用的东西都持开放态度,不一定是mrvcrt.谢谢!

Why not? I see @MatthieuRiegler already asked this at Using msvcrt.getch() in Eclipse / PyDev ... but I am open to anything that works, not necessarily mrvcrt. Thanks!

推荐答案

问题在于 PyDev/Eclipse 没有给你一个真正的终端(你的程序在没有真正"控制台的情况下启动它只是重定向输出).

The problem is that PyDev/Eclipse doesn't give you a real terminal (your program is launched without a 'real' console and it just redirects the outputs).

因此,另一种方法是检查您是否处于这种情况:

So, the alternative is checking whether you're in this scenario with:

import sys is_in_terminal = sys.stdin.isatty() if not is_in_terminal: entered = input() # input() on Py3, on Py2 it'd be raw_input() else: import msvcrt entered = msvcrt.getwch()

唯一的问题是,如果它不在终端中,则内容只能在新行上供程序使用(因此,如果不按回车键,就无法获得该输出).

The only thing is that if it's not in a terminal, the contents are only available to the program on a new line (so, it's not really possible to get that output without him pressing enter).

请注意,虽然它需要在 Eclipse 中进行一些终端模拟,但可能有一个真正的"终端——例如 marketplace.eclipse/content/tcf-terminals -- 然后 PyDev 可以在这样的终端中启动程序而不是使用控制台视图......(但是这只是在想法世界中,没有截止日期,因此,不幸的是,目前无法在 PyDev/Eclipse 中不输入的情况下获取单个字符).

Note that having a 'real' terminal could be possible, although it'd require some terminal emulation inside Eclipse -- such as marketplace.eclipse/content/tcf-terminals -- and then PyDev could launch a program in such a terminal instead of using the console view... (but this is just in the ideas world, there's not due date for that, so, unfortunately, it's not currently possible to grab a single char without an enter inside PyDev/Eclipse).

更多推荐

Pydev/Eclipse 中的单字符输入

本文发布于:2023-10-19 19:28:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   Pydev   Eclipse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!