如何在 kivy 中使用 weakref 遍历不同的 ids 级别

How to iterate over different ids levels using weakref in kivy

我有这个 ids.

序列

self.ids.cuarta_pantalla.ids.container.ids.pre_1.ids.Si

在这种情况下,容器 有 70 个不同的 ID [从 pre_1pre_70] 并且每个 pre_(x) 都有三个不同的 id [Si, MasMenos, No]对应一组复选框.

如果我想知道使用其属性 value 的单个复选框的状态,我不得不像这样编写所有语句。

self.ids.cuarta_pantalla.ids.container.ids.pre_1.ids.Si.value

那么,如何遍历 ID?

我试过使用方括号 self.ids.cuarta_pantalla.ids.container.ids['pre_1'] 但它 returns 我无法调用任何方法。

用方括号打印:<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时,一个Observable List调用children 到添加的对象中,另一个将包含该添加对象的引用。

例如:

IDlist = self.ids.cuarta_pantalla.ids.container.children

可以迭代变量 IDlist 以获取每个引用,您可以在其中调用该特定对象的任何方法