一:通过ObjectProperty获取
在my.kv里:
<MyFirstWidget>:
# both these variables can be the same name and this doesn't lead to
# an issue with uniqueness as the id is only accessible in kv.
txt_inpt: txt_inpt
Button:
id: f_but
TextInput:
id: txt_inpt
text: f_but.state
on_text: root.check_status(f_but)
from kivy.properties import ObjectProperty
class MyFirstWidget(BoxLayout):
txt_inpt = ObjectProperty(None) #txt_inpt 是kv文件里对应的id
def check_status(self, btn):
print('button state is: {state}'.format(state=btn.state))
print('text input text is: {txt}'.format(txt=self.txt_inpt))
二:通过ids获取
<Marvel>
Label:
id: loki
text: 'loki: I AM YOUR GOD!'
Button:
id: hulk
text: "press to smash loki"
on_release: root.hulk_smash()

本文介绍了在Kivy框架中如何通过ObjectProperty和ids属性两种方式来获取Widget对象。第一种方法是在KV语言文件中定义ObjectProperty,然后在Python代码中使用。第二种方法直接在Python代码中通过self.ids访问KV文件中定义的id。
最低0.47元/天 解锁文章
60

被折叠的 条评论
为什么被折叠?



