蟒蛇闹钟 [英] Python Alarm Clock

查看:58
本文介绍了蟒蛇闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哥哥的帮助下制作了这个小闹钟.我昨晚试过了,没有 nonBlockingRawInput 并且效果很好,但是使用 nonBlockingRawInput 它不起作用.今天我试过了,但它们都不起作用!我将发布带有 nonBlockingRawInput 和non"文件的代码.如果您想要没有 nonBlockingRawInput 的代码,请直接询问.

I've made this little alarm clock with a little help from my brother. I tried it last night, with out the nonBlockingRawInput and that worked fine, but with the nonBlockingRawInput it didn't work. Today I've tried it but neither of them work! I will post the code with the nonBlockingRawInput and the "non" file. If you want the code without nonBlockingRawInput, just ask.

提前致谢.

报警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 ''

推荐答案

我已经研究你的代码有一段时间了.据我所知,您希望能够运行警报,同时还能够在 shell 中键入停止"以结束程序,为此您可以将警报设为线程.该线程将检查是否该说警报!"并打开mp3.如果用户没有在 shell 中输入 stop,线程将休眠并稍后再次检查.

I've been looking at your code for a while now. As far as I can understand you want to be able to run an alarm while also being able to type "stop" in the shell to end the program, to this end you can make the alarm a thread. The thread will check if its time to say "ALARM!" and open the mp3. If the user hasn't typed stop in the shell, the thread will sleep and check again later.

我基本上使用了您的代码并将其放入警报线程类中:

I essentially used your code and just put it into an alarm thread class:

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()

值得注意的是,当线程休眠时,用:

It is worth noting, that when the thread is sleeping, with:

time.sleep(60)

并且您在 shell 中键入 stop 线程必须在它意识到它已死之前唤醒它,因此最坏的情况是您可能会等待一分钟以等待程序关闭!

And you typed stop in the shell the thread would have to wake up before it realised it was dead, so you could at worst end up waiting a minute for the program to close!

这篇关于蟒蛇闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆