【问题标题】:AttributeError: 'super' object has no attribute '__getattr__' in pythonAttributeError:'super'对象在python中没有属性'__getattr__'
【发布时间】:2020-10-06 15:52:03
【问题描述】:

我只是在做一个小项目并制作了多个屏幕(使用 kivy),在一个用户将输入他/她的体重和身高的屏幕中,我得到了一个 Attributeerror getattr。不知道我在代码中做错了什么。我是 python 新手,所以我需要帮助。

这里是python代码:

class UserHomeScrren(BoxLayout):
    

    def __init__(self, **kwargs):
        super(UserHomeScrren, self).__init__(**kwargs)
     

    def calculate(self):
        #weight =int(self.weight.text)
        weight=int(self.ids.backdrop.ids.backlayer.bmi_screen.weight.text)
        height= int (self.ids.backdrop.ids.backlayer.bmi_screen.height.text)
        print(self.ids.backlayer.ids.bmi_screen.ids.height)
        BMI = round((weight * 703) / (height ** 2))

        if BMI < 18.5:
            self.ids.results.text += "Your BMI is " + str(BMI) + " which means you are underweight."


    def clear(self):
        self.ids.results.text = ""
        self.ids.weight.text = ""
        self.ids.height.text = ""
        

        print(weight)
        print('calculate.....')


class ItemBackdropBackLayer(ThemableBehavior, BoxLayout):
    icon = StringProperty("android")
    text = StringProperty()
    selected_item = BooleanProperty(False)

    def on_touch_down(self, touch):
        if self.collide_point(touch.x, touch.y):
            for item in self.parent.children:
                if item.selected_item:
                    item.selected_item = False
            self.selected_item = True
        return super().on_touch_down(touch)


class UserApp(MDApp):
    def __init__(self, **kwargs):
        self.title = "User Flana"
        self.theme_cls.primary_palette = "DeepPurple"
        super().__init__(**kwargs)
        Window.size = (300, 550)

    def __getattr__(self, attr):
        return super().__getattr__(attr)

    def build(self):
        return UserHomeScrren()

    
if __name__ == "__main__":
    UserApp().run()

这里是日志:

   File "d:\obacity\userHome\user.py", line 38, in calculate
     weight=int(self.ids.backdrop.ids.backlayer.bmi_screen.weight.text)
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'```

【问题讨论】:

    标签: python python-3.x python-2.7 kivy


    【解决方案1】:

    我认为您甚至不需要再次重新定义__getattr__,对吧? :-)

    由于您在__init__ 中调用super(),它应该继承__getattr__ 的基类方法,它只返回所请求属性的值

    【讨论】:

    • 无论super() 是否被调用,Python 都会在 MRO 中获取下一个方法。如果子类中缺少某个方法,它会自动检查父类等。 super() 的唯一用途是,除了子类方法之外,您还想调用特定的父类方法。
    • 如果我删除 def __getattr__(self, attr): 函数,那么它会显示相同的错误。
    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多