如标题所述,使用sys.stdout.write
打印时是否有任何方法可以禁用用户输入?
-
例如,如果我使用此功能慢慢打印出字符串:
import time
import sys
def printSlowly(text, delay):
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(delay)
我如何在功能打印内容时阻止用户输入任何内容?
请注意,这是针对Linux Ubuntu终端的。
谢谢!
弄清楚了!涉及禁用回声和冲洗输入的组合。
# from https://gist.github.com/kgriffs/5726314
def enable_echo(enable):
fd = sys.stdin.fileno()
new = termios.tcgetattr(fd)
if enable:
new[3] |= termios.ECHO
else:
new[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new)
# from https://stackoverflow.com/questions/26555070/linux-python-clear-input-buffer-before-raw-input
def flushInput():
termios.tcflush(sys.stdin, termios.TCIFLUSH)