python - Python Kivy 返回'AttributeError:'super'对象没有属性'__getattr__''
问题描述
我正在尝试使用 Kivy 构建交互式学校时间表,但在根据名称更改 Button 文本时一直遇到问题。我创建了一个网格布局,其中每个按钮都有一个唯一的名称,例如星期一的第一个按钮是one_mon
下一个按钮two_mon
,依此类推。我创建了一个继承自的类,Button
这里是该类的 Kivy 和 Python 代码:
<Tile>:
background_color: [.5, .9, 1, 1]
halign: "center"
size_hint: None, None
size: 96, 96
text: self.lesson
on_press: self.on_press()
on_release: self.on_release()
这是 Tile 的 Python 代码
class Tile(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.lesson = ""
self.sub_press = ""
self.check()
self.text = self.lesson
def check(self):
if self.ids.one_mon == "one_mon":
self.text = "English"
self.sub_press = "Room nr. 42 \n Mr. Hetman"
...
else:
self.lesson = "None"
self.sub_press = "None"
def on_release(self):
self.text = self.lesson
self.background_color = [.5, .9, 1, 1]
def on_press(self):
self.text = self.sub_press
self.background_color = [.01, .9, 1, 1]
这是错误消息if self.ids.one_mon.name == "one_mon": File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__ AttributeError: 'super' object has no attribute '__getattr__'
以下是将所有这些按钮放在一起的父小部件的代码:
<PlanChart>:
cols: 11
padding: 2
Tile:
id: one_mon
name: "one_mon"
Tile:
id: two_mon
name: "two_mon"
Tile:
id: three_mon
name: "three_mon"
......
Tile:
id: ten_fri
name: "ten_fri"
为什么我会收到此错误?还有其他方法可以检查按钮的ID吗?也许我应该完全放弃这个项目并使用不同的策略重新开始?欢迎任何帮助
解决方案
问题可能来自您从Button类继承的方式以及您如何将属性绑定到按钮
您应该从按钮类继承并覆盖on_release方法
方法覆盖后,该单词self.text
将被引用为当前按钮内部文本
from kivy.properties import ObjectProperty
class Tile(Button):
def __init__(self, **kwargs):
super(Tile, self).__init__(**kwargs)
self.lesson = ""
def on_release(self):
self.text = self.lesson
推荐阅读
- javascript - Sharepoint 上的补丁更新 - 提供的参数之一是不可接受的
- javascript - 通过文档 ID 中的短语/字符串或文档中数组的内容进行 Firestore 查询
- asp.net-core - 如何在 IHealthCheck 实现中使用数据库配置对发出 http 请求的服务进行健康检查?
- java - 每次从并发哈希映射读取的性能与将值分配给变量然后使用它
- highcharts - 在highcharts中切换图表类型时如何更改滚动条位置
- gradle - 在应用所有插件之后但在“afterEvaluate()”之前在 Gradle 中执行操作
- reactjs - 如何使用从导航栏底部开始的Materail ui react 创建顶部抽屉?
- mysql - ADO 连接期间的 COMException 包括 Excel
- gradle - Gradle 未检测到多版本 jar 的兼容 JDK
- spring-boot - Springboot 客户端服务器架构