python - 如何在kivy中使用weakref迭代不同的ids级别
问题描述
我有这个ids序列。
self.ids.cuarta_pantalla.ids.container.ids.pre_1.ids.Si
在这种情况下,容器有 70 个不同的 id [从pre_1到pre_70 ],每个 pre_(x) 有三个不同的 id [ Si,MasMenos,No ],它们对应于一组CheckBoxes。
如果我想知道使用其属性值的单个复选框的状态,我不得不像这样编写所有语句。
self.ids.cuarta_pantalla.ids.container.ids.pre_1.ids.Si.value
.
那么,我怎样才能遍历 id?
我试过使用方括号self.ids.cuarta_pantalla.ids.container.ids['pre_1']
,但它返回的东西我不能调用任何方法。
用方括号打印:<weakref at 0x125F7118; to 'BoxLayout' at 0x125F30D0>
用点符号打印:<kivy.uix.boxlayout.BoxLayout object at 0x125F30D0>
这是我创建对象的方式:
for idx in range(70):
BoxContainer = BoxLayout()
# Repeat this two more times with MasMenos and No
check1 = CheckBox(group= f"p_{idx+1}")
BoxContainer.add_widget(check1)
BoxContainer.ids['Si'] = weakref.ref(check1)
#Adding the BoxContainer with CheckBoxes to the container
self.ids.cuarta_pantalla.ids.container.add_widget(BoxContainer)
self.ids.cuarta_pantalla.ids.container.ids[f'pre_{idx+1}'] = weakref.ref(BoxContainer)
解决方案
不需要使用weakref,每次add_widget
使用时,一个称为children的Observable List将包含在添加的对象中,另一个将包含该添加对象的引用。
例如:
IDlist = self.ids.cuarta_pantalla.ids.container.children
可以迭代变量IDlist以获取每个引用,您可以在其中调用该特定对象的任何方法
推荐阅读
- postgresql - Hibernate / Postgres - 更改默认列类型映射
- android - Kotlin中的高阶函数如何使用另一个参数作为函数类型参数的参数
- c - 如何将数据动态存储在C中的链表中?
- reactjs - 父组件的 ReactJS 状态仅更新子组件而不更新父组件
- python - 我是 python 编程的新手,我需要通过用户输入来计算用户的名字,他们的名字长度超过 5 个字符
- python - 将两个熊猫数据框组合在一起Python
- amazon-web-services - AWS - “我们计算的请求签名与您提供的签名不匹配”与 IAM 用户和来自 python 的请求
- powerbi - Power BI 嵌入 React Js
- python-3.x - 通过scipy中的稀疏矩阵访问元素
- c++ - 为 std::deque 使用磁盘分配器