그것은 당신이 사용중인 운영 체제를 알고 도움이 될 것이다, 이것은 매우 운영 체제 관련 질문은한다. 예를 들어 sys.stdin에 fileno 특성이 없으므로 Kylar의 대답은 Windows에서 작동하지 않습니다.
나는 호기심과 함께 저주를 사용하여 솔루션을 던졌다, 그러나 이것은 윈도우 중 하나에서 작동하지 않습니다 :
#!/usr/bin/python
import time
import sys
import curses
def alarmloop(stdscr):
stdscr.addstr("How many seconds (alarm1)? ")
curses.echo()
alarm1 = int(stdscr.getstr())
while (1):
time.sleep(alarm1)
curses.flushinp()
stdscr.clear()
stdscr.addstr("Alarm1\n")
stdscr.addstr("Continue (Y/N)?[Y]:")
doit = stdscr.getch()
stdscr.addstr("\n")
stdscr.addstr("Input "+chr(doit)+"\n")
stdscr.refresh()
if doit == ord('N') or doit == ord('n'):
stdscr.addstr("Exiting.....\n")
break
curses.wrapper(alarmloop)
편집 : 아, 윈도우. 그런 다음 msvcrt 모듈을 사용할 수 있습니다. 아래의 코드가 완벽하지 않습니다, 그것은 전혀 IDLE에서 작동하지 않습니다
#!/usr/bin/python
import time
import subprocess
import sys
import msvcrt
alarm1 = int(raw_input("How many seconds (alarm1)? "))
while (1):
time.sleep(alarm1)
print "Alarm1"
sys.stdout.flush()
# Try to flush the buffer
while msvcrt.kbhit():
msvcrt.getch()
print "Continue (Y/N)?[Y]"
doit = msvcrt.getch()
print "Input",doit
if doit == 'N' or doit=='n':
print "Exiting....."
break
'sys.stdout.flush(); 뒤에 세미콜론이 필요 없다. –
내 잘못이야, 네 말이 맞아, 나는 세미콜론이 필요 없어. 코드가 수정되었습니다. – Amjith
"모든 키 스트로크를 플러시하거나 버립니다"? "읽기 및 무시"를 의미합니까? 입력을 쉽게 "플러시"할 수 없습니다. 단순하고 분명한 것은하지 말고 읽은 다음 무시하십시오. –