Python警报C

2024-06-10 04:10:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在哥哥的帮助下做了这个小闹钟。我昨天晚上试过了,没有nonBlockingRawInput效果很好,但是没有nonBlockingRawInput效果。今天我试过了,但都不管用!我将用nonBlockingRawInput和“non”文件发布代码。如果您想要没有nonBlockingRawInput的代码,请直接询问。

提前谢谢。

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

非.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 ''

Tags: tonameimportinputsignalhhtimeoutnow
2条回答

我已经看了你的代码一段时间了。据我所知,您希望能够运行一个警报,同时也能够在shell中键入“stop”来结束程序,为此,您可以使警报成为一个线程。线程将检查是否该说“警报!”打开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中键入stop,线程必须在意识到它已经死之前醒来,这样最坏的情况下,您可能会等待一分钟,等待程序关闭!

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)

相关问题 更多 >