def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
现在我可以pause()在需要的时候使用该函数,就像编写批处理文件一样。例如,在这样的程序中:
import os
import system
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")print("Think about what you ate for dinner last night...")
pause()
So, I found this to work very well in my coding endeavors. I simply created a function at the very beginning of my program,
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
and now I can use the pause() function whenever I need to just as if I was writing a batch file. For example, in a program such as this:
import os
import system
def pause():
programPause = raw_input("Press the <ENTER> key to continue...")
print("Think about what you ate for dinner last night...")
pause()
Now obviously this program has no objective and is just for example purposes, but you can understand precisely what I mean.
NOTE: For Python 3, you will need to use input as opposed to raw_input
回答 4
我有一个类似的问题,我正在使用信号:
import signal
def signal_handler(signal_number, frame):print"Proceed ..."
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
So you register a handler for the signal SIGINT and pause waiting for any signal. Now from outside your program (e.g. in bash), you can run kill -2 <python_pid>, which will send signal 2 (i.e. SIGINT) to your python program. Your program will call your registered handler and proceed running.
回答 5
我在python2和3中使用以下命令来暂停代码执行,直到用户按下ENTER
import six
if six.PY2:
raw_input("Press the <ENTER> key to continue...")else:
input("Press the <ENTER> key to continue...")
对于较长的文本块,最好使用input('Press <ENTER> to continue')(或raw_input('Press <ENTER> to continue')在Python 2.x上)提示用户,而不是延迟时间。快速的阅读器不想等待延迟,慢速的阅读器可能希望在延迟上花费更多的时间,某人在阅读该延迟时可能会被打扰,并且需要更多的时间,依此类推。此外,如果某人经常使用该程序,他/她可能已经习惯了它的工作原理,甚至不需要阅读冗长的文字。让用户控制将文本块显示多长时间以供阅读,这是更友好的做法。
As pointed out by mhawke and steveha‘s comments, the best answer to this exact question would be:
For a long block of text, it is best to use input('Press <ENTER> to continue') (or raw_input('Press <ENTER> to continue') on
Python 2.x) to prompt the user, rather than a time delay. Fast readers
won’t want to wait for a delay, slow readers might want more time on
the delay, someone might be interrupted while reading it and want a
lot more time, etc. Also, if someone uses the program a lot, he/she
may become used to how it works and not need to even read the long
text. It’s just friendlier to let the user control how long the block
of text is displayed for reading.
import getpass
getpass.getpass("Press Enter to Continue")
It hides whatever the user types in, which helps clarify that input is not used here.
But be mindful on the OS X platform. It displays a key which may be confusing.
Probably the best solution would be to do something similar to the getpass module yourself, without making a read -s call. Maybe making the foreground color match the background?
回答 11
通过这种方法,只需按以下指定的任何键即可恢复程序:
import keyboard
whileTrue:
key = keyboard.read_key()if key =='space':# you can put any key you like instead of 'space'break
相同的方法,但以另一种方式:
import keyboard
whileTrue:if keyboard.is_pressed('space'):# same, you can put any key you like instead of 'space'break