树莓派项目的Python闹钟代码问题

时间:2017-01-08 21:32:12

标签: python raspberry-pi clock alarm

我已经为我正在研究的项目(咖啡酿造闹钟)的闹钟部分编写了这段代码。当我运行程序时,它只是跳到“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()

1 个答案:

答案 0 :(得分:1)

我不完全确定您在尝试使用该结束循环时做了什么,但我相信它给您错误的原因是因为您尝试引用user_input就好像这是一个功能。也许你试图等待用户输入一些东西?如果是的话......

尝试更改此内容:

text = str(user_input())

到此:

text = str(input(''))