【问题标题】:I don't know why I keep getting the error 'str' has not attribute 'message'我不知道为什么我不断收到错误 'str' has no attribute 'message'
【发布时间】:2020-12-15 00:18:09
【问题描述】:
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")

我得到的错误如下:

Traceback(最近一次调用最后一次): 文件“c:/Users/yomamahahaha/Desktop/oooohackrrr/actual programs/python/Reminders/reminder.py”,第 28 行,在 Reminder.sendNotification("kek") 文件“c:/Users/yomamahahaha/Desktop/oooohackrrr/actual programs/python/Reminders/reminder.py”,第 20 行,在 sendNotification 打印(self.message) AttributeError: 'str' 对象没有属性 'message'

【问题讨论】:

  • 如何共享实际上给您错误的代码部分?您给我们的内容中没有第 28 行。您也没有初始化该类的实例(假设 Reminder 不是变量,而是试图实例化该类)。您还将消息传递给不带参数的 sendNotification 方法。应该是这样的:Reminder('message').sendNotification().

标签: python class attributeerror self


【解决方案1】:

您没有创建提醒的实例

所以当你打电话时

Reminder.sendNotification("message")

您在这里将 'self' 设置为“消息”:然后调用 "message".message ,它不存在

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

我想你是说

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多