button_child_A
で定義を呼び出すボタン ( ) がありますon_press
。この定義では、3 つのラベルのテキストを変更したいと考えています。
- `label_child_A` is in the same class as the `button_child_A`
- `label_parent` is in the parent class of `button_child_A`
- `label_child_B` is in the sister class of `button_child_A` (same parent class)
を変更label_child_A
して、 label_parent
then を呼び出すだけで変更できますが、その を呼び出してids
変更することはできません。エラーが発生します: 。(下記参照)label_child_B
id
"object has no attribute"
def change_label
.py
を作成する必要があることは理解していますObjectProperty
が、なぜですか? ObjectProperty
同じクラスまたは親クラスからウィジェットを変更するときは必要ないのに、「姉妹クラス」(同じ親) からウィジェットを変更するときに必要なのはなぜですか?
.py
:_
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class Class_Parent(BoxLayout):
pass
class Class_Child_A(BoxLayout):
def change_labels(self):
self.ids.label_child_A.text = "changed"
self.parent.ids.label_parent.text= "changed"
#self.parent.ids.classAb_id.label_child_B.text = "changed" ## get the error 'Class_Child_B' object has no attribute 'label_child_B'
self.parent.ids.classAb_id.label_child_b_object_property.text = "changed" # why do I need to use ObjectProperty here ?
pass
class Class_Child_B(BoxLayout):
child_b_label_object_property = ObjectProperty(None)
class TestApp(App):
def build(self):
return Class_Parent()
TestApp().run()
そして.kv
:
<Class_Parent>:
Label:
id: label_parent
text: "label parent"
Class_Child_A:
id: classAa_id
Class_Child_B:
id: classAb_id
<Class_Child_A>:
Button:
id: button_child_A
text: "button child A"
on_press: root.change_labels()
Label:
id: label_child_A
text: "label child A"
<Class_Child_B>:
label_child_b_object_property: label_child_B
Label:
id: label_child_B
text: "label child B"