Ik wil dat mijn script wacht tot de gebruiker op een toets drukt.
Hoe doe ik dat?
In Python 3 bestaat raw_input()
niet. Dus, gewoon gebruiken:
input("Press Enter to continue...")
In Python 2, moet je raw_input()
gebruiken, omdat input(prompt)
gelijk is aan eval(raw_input(prompt))
:
raw_input("Press Enter to continue...")
Dit wacht echter alleen op een gebruiker die op enter drukt, dus je kunt misschien beter msvcrt gebruiken (alleen Windows/DOS) De msvcrt module geeft je toegang tot een aantal functies in de Microsoft Visual C/C++ Runtime Library (MSVCRT)):
import msvcrt as m
def wait():
m.getch()
Dit moet wachten op een druk op een toets.
Een manier om dit in Python 2 te doen, is door raw_input()
te gebruiken:
raw_input("Press Enter to continue...")
In python3 is het gewoon input()
De python handleiding geeft het volgende:
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", repr(c)
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
die in uw use case kan worden gerold.