Python 等待 x 秒等待某个键,如果没有按下则继续执行
我正在寻找执行以下操作的代码片段/示例:
- 显示一条消息,例如“按任意键进行配置或等待 X 秒继续”
- 等待,例如 5 秒并继续执行,或输入 configure() 子例程如果按下某个键。
I'm looking for a code snippet/sample which performs the following:
- Display a message like "Press any key to configure or wait X seconds to continue"
- Wait, for example, 5 seconds and continue execution, or enter a configure() subroutine if a key is pressed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用的是 Unix/Linux,那么 select 模块将为您提供帮助。
如果您使用的是 Windows,请查看 msvcrt 模块。 (请注意,这在 IDLE 中不起作用,但在 cmd 提示符下有效)
If you're on Unix/Linux then the select module will help you.
If you're on Windows, then look into the msvcrt module. (Note this doesn't work in IDLE, but will in cmd prompt)
Python 没有任何标准方法来捕获此问题,它只能通过 input() 和 raw_input() 获取键盘输入。
如果你真的想要这个,你可以使用 Tkinter 或 pygame 将击键捕获为“事件”。还有一些特定于平台的解决方案,例如 pyHook。但如果它对你的程序不是绝对重要,我建议你让它以另一种方式工作。
Python doesn't have any standard way to catch this, it gets keyboard input only through input() and raw_input().
If you really want this you could use Tkinter or pygame to catch the keystrokes as "events". There are also some platform-specific solutions like pyHook. But if it's not absolutely vital to your program, I suggest you make it work another way.
如果将 time.sleep、threading.Thread 和 sys.stdin.read 结合起来,您可以轻松等待指定的输入时间,然后继续。
If you combine time.sleep, threading.Thread, and sys.stdin.read you can easily wait for a specified amount of time for input and then continue.
我是这样做的:
我的方法不是让阻塞调用超时,而是启动一个线程等待用户输入输入,而另一个线程则执行其他操作。这两个进程通过少量原子操作进行通信:在本例中,设置一个布尔标志。对于比原子操作更复杂的事情,显然您应该用某种线程安全缓冲区替换原子变量。
Here's how I did it:
Rather than make a blocking call time out, my approach is to start a thread that waits for the user to enter input, while another thread does something else. The two processes communicate through a small number of atomic operations: in this case, setting a boolean flag. For anything more complicated than atomic operations, obviously you should replace the atomic variable with a threadsafe buffer of some kind.
基于 https://note.nkmk.me/en/python-while 的想法-usage/,以及来自其他地方的。
如果 5 秒内按下 CTRL-C,程序将停止,否则将继续。
适用于 Python 3,不需要任何外部(pip install ...)库。
应该可以在 Linux 和 Windows 上运行。
如果您希望程序更频繁地检查用户输入,请在 time.sleep() 之前注释 print 函数,并将 time.sleep(1) 更改为 time.sleep(0.1)。您可能也会使用顶部打印功能。
注意:
使用它在一行中打印所有内容:
使用它在函数内继续:
Based on idea from https://note.nkmk.me/en/python-while-usage/, and from elsewhere.
Program will stop if CTRL-C is pressed withing 5 seconds, otherwise will continue.
Works on Python 3, does not need any external (pip install ...) libraries.
Should work on Linux and Windows.
If you wish for program to check user input more often, comment print function before time.sleep(), and change time.sleep(1) to time.sleep(0.1). You would probably use top print function, too.
note:
use this to print all in one line:
use this to continue inside a function: