Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

python - 如何使python等待按键(How do I make python to wait for a pressed key)

I want my script to wait until the user presses any key.

(我希望脚本等待用户按下任何键。)

How do I do that?

(我怎么做?)

  ask by Janusz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Python 3, no raw_input() exists.

(在Python 3中,不存在raw_input() 。)

So, just use:

(因此,只需使用:)

input("Press Enter to continue...")

In Python 2, you should use raw_input() , as input(prompt) is equivalent to eval(raw_input(prompt)) :

(在Python 2中,您应该使用raw_input() ,因为input(prompt)等效于eval(raw_input(prompt)) :)

raw_input("Press Enter to continue...")

This only waits for a user to press enter though, so you might want to use msvcrt ((Windows/DOS only) The msvcrt module gives you access to a number of functions in the Microsoft Visual C/C++ Runtime Library (MSVCRT)):

(但是,这仅等待用户按Enter键,因此您可能要使用msvcrt ((仅适用于Windows / DOS)使用msvcrt模块可以访问Microsoft Visual C / C ++运行时库(MSVCRT)中的许多功能):)

import msvcrt as m
def wait():
    m.getch()

This should wait for a key press.

(这应该等待按键。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...