首页 > 解决方案 > AttributeError:'super'对象在python中没有属性'__getattr__'

问题描述

我只是在制作一个小项目并制作了多个屏幕(使用 kivy),在一个用户将输入他/她的体重和身高的屏幕中,我得到了getattrAttributeerror。不知道我在代码中做错了什么。我是 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__'```

标签: pythonpython-3.xpython-2.7kivy

解决方案


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

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


推荐阅读