发布于2019-09-11 19:44 阅读(904) 评论(0) 点赞(1) 收藏(1)
我想知道什么?
在这个链接中解释(很少),但不是从初学者的角度来解释。
我有2个文件
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类)的Label。
如果按下具有id:button_b(Get_Boys类)的Button,则必须更改具有id:root_lbl(Get_People类)的Label。
在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 =“男孩改变了女孩!”
并在.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,则必须更改具有id:label_b(Get_Boys类)的Label。
在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()
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/108053/72baa5836d960979e727/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!