我從哥哥那裏得到了一點小小的鬧鐘。我昨晚試了一下,出nonBlockingRawInput
,這工作正常,但與nonBlockingRawInput
它沒有工作。今天我試過了,但都沒有工作!我將用nonBlockingRawInput
和「非」文件發佈代碼。如果你想要沒有nonBlockingRawInput
的代碼,只需詢問。Python鬧鐘
在此先感謝。
報警rpi.py:
import time
import os
from non import nonBlockingRawInput
name = input("Enter your name.")
print("Hello, " + name)
alarm_HH = input("Enter the hour you want to wake up at")
alarm_MM = input("Enter the minute you want to wake up at")
print("You want to wake up at " + alarm_HH + ":" + alarm_MM)
while True:
now = time.localtime()
if now.tm_hour == int(alarm_HH) and now.tm_min == int(alarm_MM):
print("ALARM NOW!")
os.popen("open mpg321 /home/pi/voltage.mp3")
break
else:
print("no alarm")
timeout = 60 - now.tm_sec
if nonBlockingRawInput('', timeout) == 'stop':
break
non.py:
import signal
class AlarmException(Exception):
pass
def alarmHandler(signum, frame):
raise AlarmException
def nonBlockingRawInput(prompt='', timeout=20):
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(timeout)
try:
text = input(prompt)
signal.alarm(0)
return text
except AlarmException:
pass
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''
把代碼放在這裏。 – Marcin 2013-05-01 19:35:43
你可能想給它一個時間範圍,以便它可以觸發,如果它的支票沒有完全打到時間 – Chachmu 2013-05-01 19:40:03
那麼它不起作用? – 2013-05-01 19:43:00