首页 > 解决方案 > 我不知道为什么我不断收到错误 'str' has not attribute 'message'

问题描述

from plyer import notifications

class Reminder():
    def __init__(self,message = None):

        if not message:
            message = input("Please enter the message: ")

        self.message = message
        print(self.message)

    def sendNotification(self):
        print(self.message)
        notification.notify(title = "Python reminder", message = self.message, app_name = "Python reminder program",app_icon = "",timeout = 10,ticker = "this is ticker thing")

    def getTime(self):
        pass



Reminder.sendNotification("message")

我得到的错误如下:

回溯(最近一次通话最后):文件“c:/Users/yomamahahaha/Desktop/oooohackrrr/actualprograms/python/Reminders/reminder.py”,第 28 行,在 Reminder.sendNotification("kek") 文件“c:/用户/yomamahahaha/Desktop/oooohackrrr/actual programs/python/Reminders/reminder.py",第 20 行,在 sendNotification print(self.message) AttributeError: 'str' object has no attribute 'message'

标签: pythonclassattributeerrorself

解决方案


您不创建 Reminder 的实例

所以当你打电话时

Reminder.sendNotification("message")

您在此处将“自我”设置为“消息”:然后调用"message".message,不存在

def sendNotification(self):
    print(self.message)

我想你的意思是

myReminder = Reminder("Message")
myReminder.sendNotification()

推荐阅读