我想知道什么?
- 如果释放具有id:button_b(Get_Boys类)的按钮,则必须更改具有id:label_g(Get_Girls类)的Label。
- 如果按下具有id:button_b(Get_Boys类)的Button,则必须更改具有id:root_lbl(Get_People类)的Label。
- 如果释放了具有id:root_btn(Get_People类)的Button,则必须更改具有id:label_b(Get_Boys类)的Label。
它被解释为(很少) 这个 链接,但不是从初学者的角度来看。
我有2个文件
- test.py
- dates_test.kv
test.py
class Get_People(BoxLayout):
pass
class Get_Boys(BoxLayout):
pass
class Get_Girls(BoxLayout):
pass
class TestApp(App):
def build(self):
self.load_kv('dates_test.kv')
return Get_People()
dates_test.kv文件
<Get_People>:
orientation: 'vertical'
Button:
name: root_btn
id: root_btn
text: "I am Root Button"
on_release: change_label_b
Label:
id: root_lbl
text: "I am Root Label"
Get_Boys:
Get_Girls:
<Get_Boys>:
Button:
id: button_b
text: "Button for boys"
on_press: change_label_root
on_release: change_label_g
Label:
id: label_b
text: "Label for boys"
<Get_Girls>:
Button:
id: button_g
text: "Button for girls"
Label:
id: label_g
text:"Label for girls"
好吧!看起来我自己找到了答案,我想分享一下。
首先让我们在dates_test.kv文件中给出“id”。这样您就可以在python代码或.kv文件中访问它们。
<Get_People>:
stuff_p: root_lbl
...
Get_Boys:
id: gb
Get_Girls:
id: gg
<Get_Boys>:
stuff_b: label_b
<Get_Girls>:
stuff_c: label_g
你可能想知道什么是stuff_p,stuff_b和stuff_c ???
它们是在自己的类中定义的ObjectProperty。您在python代码中的stuff_b中所做的更改会在label_b中进行更改,因为您已在kivy文件中进行了链接。
class Get_People(BoxLayout):
stuff_p = ObjectProperty(None)
...
class Get_Boys(BoxLayout):
stuff_b = ObjectProperty(None)
...
class Get_Girls(BoxLayout):
stuff_c = ObjectProperty(None)
...
对于第1部分和第2部分
如果释放了具有id:button_b(Get_Boys类)的按钮,则标签
id:label_g(Get_Girls类)必须更改。
如果按下具有id:button_b(Get_Boys类)的Button,则标签
id:root_lbl(Get_People类)必须更改。
在Get_Boys类(test.py)中定义这些方法。
def change_girl(self):
self.parent.ids.gg.stuff_c.text = "Boys changed Girls!"
#self.stuff_b.text = "i changed myself!"
def change_people(self):
self.parent.ids.root_lbl.text = "Boys changed people!"
让我们看看这里发生了什么......
self.parent.ids.gg.stuff_c.text =“男孩改变了女孩!”
- self.parent指的是Get_Parent类。
- .ids.gg是指我们为Get_Girls创建的id。
- .stuff_c用于在Get_Girls类中引用label_g(上面)。
- .text用于更改标签中的文本。
并在.kv文件中
<Get_Boys>:
stuff_b: label_b
Button:
id: button_b
text: "button 1"
on_release: root.change_girl()
on_press: root. change_people()
对于第3部分
- 如果释放了具有id:root_btn(Get_People类)的Button,则为Label
id:label_b(Get_Boys类)必须更改。
在Get_People类(test.py)中定义一个方法。
def rooted(self):
self.ids.gb.stuff_b.text = "people changed boys!"
并在.kv文件中
Button:
id: root_btn
text: "I am Root"
on_release: root.rooted()
好吧!看起来我自己找到了答案,我想分享一下。
首先让我们在dates_test.kv文件中给出“id”。这样您就可以在python代码或.kv文件中访问它们。
<Get_People>:
stuff_p: root_lbl
...
Get_Boys:
id: gb
Get_Girls:
id: gg
<Get_Boys>:
stuff_b: label_b
<Get_Girls>:
stuff_c: label_g
你可能想知道什么是stuff_p,stuff_b和stuff_c ???
它们是在自己的类中定义的ObjectProperty。您在python代码中的stuff_b中所做的更改会在label_b中进行更改,因为您已在kivy文件中进行了链接。
class Get_People(BoxLayout):
stuff_p = ObjectProperty(None)
...
class Get_Boys(BoxLayout):
stuff_b = ObjectProperty(None)
...
class Get_Girls(BoxLayout):
stuff_c = ObjectProperty(None)
...
对于第1部分和第2部分
如果释放了具有id:button_b(Get_Boys类)的按钮,则标签
id:label_g(Get_Girls类)必须更改。
如果按下具有id:button_b(Get_Boys类)的Button,则标签
id:root_lbl(Get_People类)必须更改。
在Get_Boys类(test.py)中定义这些方法。
def change_girl(self):
self.parent.ids.gg.stuff_c.text = "Boys changed Girls!"
#self.stuff_b.text = "i changed myself!"
def change_people(self):
self.parent.ids.root_lbl.text = "Boys changed people!"
让我们看看这里发生了什么......
self.parent.ids.gg.stuff_c.text =“男孩改变了女孩!”
- self.parent指的是Get_Parent类。
- .ids.gg是指我们为Get_Girls创建的id。
- .stuff_c用于在Get_Girls类中引用label_g(上面)。
- .text用于更改标签中的文本。
并在.kv文件中
<Get_Boys>:
stuff_b: label_b
Button:
id: button_b
text: "button 1"
on_release: root.change_girl()
on_press: root. change_people()
对于第3部分
- 如果释放了具有id:root_btn(Get_People类)的Button,则为Label
id:label_b(Get_Boys类)必须更改。
在Get_People类(test.py)中定义一个方法。
def rooted(self):
self.ids.gb.stuff_b.text = "people changed boys!"
并在.kv文件中
Button:
id: root_btn
text: "I am Root"
on_release: root.rooted()