2013-05-01 215 views
5

我從哥哥那裏得到了一點小小的鬧鐘。我昨晚試了一下,出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 '' 
+2

把代碼放在這裏。 – Marcin 2013-05-01 19:35:43

+0

你可能想給它一個時間範圍,以便它可以觸發,如果它的支票沒有完全打到時間 – Chachmu 2013-05-01 19:40:03

+3

那麼它不起作用? – 2013-05-01 19:43:00

回答

7

我一直在看着你一會兒代碼。據我所知,你希望能夠運行一個警報,同時還能夠在shell中輸入「stop」來結束程序,爲此你可以使警報成爲一個線程。線程會檢查是否有時間說「ALARM!」並打開mp3。如果用戶沒有在shell中鍵入stop,線程將會休眠並稍後再檢查。

我基本上是用你的代碼,只是把它變成一個報警線程類:

import time 
import os 
import threading 


class Alarm(threading.Thread): 
    def __init__(self, hours, minutes): 
     super(Alarm, self).__init__() 
     self.hours = int(hours) 
     self.minutes = int(minutes) 
     self.keep_running = True 

    def run(self): 
     try: 
      while self.keep_running: 
       now = time.localtime() 
       if (now.tm_hour == self.hours and now.tm_min == self.minutes): 
        print("ALARM NOW!") 
        os.popen("voltage.mp3") 
        return 
      time.sleep(60) 
     except: 
      return 
    def just_die(self): 
     self.keep_running = False 



name = raw_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: {0:02}:{1:02}").format(alarm_HH, alarm_MM) 


alarm = Alarm(alarm_HH, alarm_MM) 
alarm.start() 

try: 
    while True: 
     text = str(raw_input()) 
     if text == "stop": 
      alarm.just_die() 
      break 
except: 
    print("Yikes lets get out of here") 
    alarm.just_die() 

值得注意的是,當線程處於休眠狀態,有:

time.sleep(60) 

你鍵入停止在shell中,線程在它意識到它已經死亡之前不得不醒來,所以最終可能最終等待一分鐘以便程序關閉!

0
import winsound,time 

a= int(input("Enter how many times I have beep :")) 
b= int(input("Enter when to wake up (in seconds) :")) 

time.sleep(b) 

for i in range(a): 
    winsound.Beep(3000,100) 
    winsound.Beep(2500,100) 
    winsound.Beep(2000,100)  
    winsound.Beep(1000,100)  
    winsound.Beep(500,100)