如何从一个 kivy 文件 (.kv) 访问不同 class 的 id/widget?
How to access id/widget of different class from a kivy file (.kv)?
我想知道什么?
- 如果id为button_b(Get_Boysclass)的按钮被释放,则id为label_g(Get_Girls[=57=的Label ]) 必须改变。
- 如果按下 ID 为 button_b (Get_Boys class) 的按钮,则按下 ID 为 root_lbl (Get_People class) 必须改变。
- 如果id为root_btn(Get_Peopleclass)的Button被释放,则id为label_b(Get_Boysclass) 必须改变。
在thislink中有解释(少),但不是从初学者的角度出发。
我有 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???
它们是在自己的 classes 中定义的 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 部分
If button with id: button_b (Get_Boys class) is released, then Label
with id: label_g (Get_Girls class) must change.
If Button with id: button_b (Get_Boys class) is pressed, then Label
with id: root_lbl (Get_People class) must change.
在Get_Boysclass(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 = "Boys changed Girls!"
- self.parent指的是Get_Parentclass.
- .ids.gg指的是我们上面为Get_Girls.
创建的id
- .stuff_c 用于引用 Get_Girls class.
中的 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 部分
- If Button with id: root_btn (Get_People class) is released, then Label
with id: label_b (Get_Boys class) must change.
在Get_Peopleclass(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()
我想知道什么?
- 如果id为button_b(Get_Boysclass)的按钮被释放,则id为label_g(Get_Girls[=57=的Label ]) 必须改变。
- 如果按下 ID 为 button_b (Get_Boys class) 的按钮,则按下 ID 为 root_lbl (Get_People class) 必须改变。
- 如果id为root_btn(Get_Peopleclass)的Button被释放,则id为label_b(Get_Boysclass) 必须改变。
在thislink中有解释(少),但不是从初学者的角度出发。
我有 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???
它们是在自己的 classes 中定义的 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 部分
If button with id: button_b (Get_Boys class) is released, then Label with id: label_g (Get_Girls class) must change.
If Button with id: button_b (Get_Boys class) is pressed, then Label with id: root_lbl (Get_People class) must change.
在Get_Boysclass(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 = "Boys changed Girls!"
- self.parent指的是Get_Parentclass.
- .ids.gg指的是我们上面为Get_Girls. 创建的id
- .stuff_c 用于引用 Get_Girls class. 中的 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 部分
- If Button with id: root_btn (Get_People class) is released, then Label with id: label_b (Get_Boys class) must change.
在Get_Peopleclass(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()