python - AttributeError:'super'对象在python中没有属性'__getattr__'
问题描述
我只是在制作一个小项目并制作了多个屏幕(使用 kivy),在一个用户将输入他/她的体重和身高的屏幕中,我得到了getattr的Attributeerror。不知道我在代码中做错了什么。我是 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__'```
解决方案
我认为你甚至不需要重新定义__getattr__
,对吧?:-)
由于您调用它应该继承基类方法,super()
该方法只返回请求的属性的值__init__
__getattr__
推荐阅读
- python - Python 哈希不匹配
- sql-server - 我需要使用在一列中具有不同值的触发器批量插入行,例如 1000 行
- c - 销毁双线程二叉树
- python - 查找元组中每个唯一元素的频率(不使用任何列表概念)并与其单词连接
- postgresql - 如何在未将数据从具有流集管道的 CSV 文件插入到 PostgresDB 时解决此问题
- java - 仅在 VSCode 下可以从多个模块问题中访问该包
- angular - 将过滤器选项传递给Angular中的路由
- sql-server - Visual Studio 社区:如何在“SQL Server 对象资源管理器”中查看数据或运行 SQL
- ubuntu-16.04 - 安装 Python 3.7 后遇到 Python 3.5 问题
- mongodb - Mongoose - 无法插入字典类型的子文档