2013-05-01 217 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)