【问题标题】:Python Alarm Clock code issue for raspberry pi project树莓派项目的 Python 闹钟代码问题
【发布时间】:2017-05-23 03:07:56
【问题描述】:

我已经为我正在处理的项目(咖啡冲泡闹钟)的闹钟部分编写了此代码。当我运行程序时,它要么只是跳到“yikes ...”部分,要么返回错误

AttributeError: 'str' 对象没有属性 'start'

有没有人知道如何解决这个问题并让闹钟正常工作?我只需要一双新的眼睛,因为我还是 python 新手,而且已经看代码太久了。

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("bensound-dubstep.mp3")
                    return
            time.sleep(60)
        except:
            return
    def just_die(self):
        self.keep_running = False



print("Enter your name: ")
user_input=input(":")

print("Hello, " + user_input)

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).format(alarm_HH, alarm_MM))

alarm=("class Alarm")
class Alarm (Alarm(alarm_HH, alarm_MM)):
    alarm.start()

try:
    while True:
         text = str(user_input())
         if text == "stop":
            alarm.just_die()
            break

except:
    print("Yikes lets get out of here")
    alarm.just_die()

【问题讨论】:

标签: python raspberry-pi clock alarm


【解决方案1】:

我不完全确定你想用那个结束循环做什么,但我相信它给你这个错误的原因是因为你试图引用 user_input,就好像它是一个函数一样。也许你只是想等待用户输入一些东西?如果是这样……

尝试改变这个:

text = str(user_input())

到这里:

text = str(input(''))

【讨论】:

  • 好的,谢谢。这使程序不会崩溃,但现在需要回击才能激活“立即报警!”并且没有声音播放。有什么想法吗?
  • 而结束循环的想法只是给用户一种通过“停止”命令关闭程序的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
  • 2016-05-15
  • 1970-01-01
  • 2014-08-15
相关资源
最近更新 更多