在 kivy 中重用代码
Reusing code in kivy
如何在 kv lang 中重用此类代码?
<Search_type_panel>:
max1_optn: max1
max2_optn: max2
both_optn: both
BoxLayout:
padding: "10dp"
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
CheckBox:
id: max1
active: True
group: 'search_type'
Label:
text: "[ref=max1]max1[/ref]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max1)
CheckBox:
id: max2
active: False
group: 'search_type'
Label:
text: "[ref=max2]max2[/ref]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
CheckBox:
id: both
active: False
group: 'search_type'
Label:
text: "[ref=both]both[/both]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
如您所见,Label 和 Checkbox 可以是一个分组的复合面板,我只需要为每个面板提供不同的参数,所以长期 运行 维护很简单,但是我如何通过新的这些家伙的参数?虽然我知道我可以将它们分组为:
<custom>:
CheckBox:
id: both
active: False
group: 'search_type'
Label:
text: "[ref=both]both[/both]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
我真的不知道如何将新参数传递给小部件并重用 kv lang 代码。
在 kv 文件中重用代码
Dynamic Classes
因为每个 CheckBox 都有不同的 活动值 (True 或 False),不建议创建同时包含 CheckBox 和 [=36= 的动态 class ]Label因为很难引用CheckBox并分配不同的id和当它们被实例化为 children.
时,它们中的每一个的活动值
片段
<Custom>:
CheckBox:
active: False
group: 'search_type'
Label:
size_hint_x: "10"
markup: True
on_ref_press: app.root.setstatus(self)
例子
下面的示例分别为 CheckBox 和 Label 创建两个动态 classes。
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class Search_type_panel(BoxLayout):
max1_optn = ObjectProperty(None)
max2_optn = ObjectProperty(None)
both_optn = ObjectProperty(None)
def setstatus(self, label):
print("\nsetstatus:")
for key in label.refs:
print("\tUser clicked on refs =", key)
class TestApp(App):
def build(self):
return Search_type_panel()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
<CustomCheckBox@CheckBox>:
active: False
group: 'search_type'
<CustomLabel@Label>:
size_hint_x: "10"
markup: True
on_ref_press: app.root.setstatus(self)
<Search_type_panel>:
max1_optn: max1
max2_optn: max2
both_optn: both
BoxLayout:
padding: "10dp"
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
CustomCheckBox:
id: max1
active: True
CustomLabel:
text: "[ref=max1]max1[/ref]"
CustomCheckBox:
id: max2
CustomLabel:
text: "[ref=max2]max2[/ref]"
CustomCheckBox:
id: both
CustomLabel:
text: "[ref=both]both[/ref]"
输出
如何在 kv lang 中重用此类代码?
<Search_type_panel>:
max1_optn: max1
max2_optn: max2
both_optn: both
BoxLayout:
padding: "10dp"
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
CheckBox:
id: max1
active: True
group: 'search_type'
Label:
text: "[ref=max1]max1[/ref]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max1)
CheckBox:
id: max2
active: False
group: 'search_type'
Label:
text: "[ref=max2]max2[/ref]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
CheckBox:
id: both
active: False
group: 'search_type'
Label:
text: "[ref=both]both[/both]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
如您所见,Label 和 Checkbox 可以是一个分组的复合面板,我只需要为每个面板提供不同的参数,所以长期 运行 维护很简单,但是我如何通过新的这些家伙的参数?虽然我知道我可以将它们分组为:
<custom>:
CheckBox:
id: both
active: False
group: 'search_type'
Label:
text: "[ref=both]both[/both]"
size_hint_x: "10"
markup: True
on_ref_press: root.setstatus(max2)
我真的不知道如何将新参数传递给小部件并重用 kv lang 代码。
在 kv 文件中重用代码
Dynamic Classes
因为每个 CheckBox 都有不同的 活动值 (True 或 False),不建议创建同时包含 CheckBox 和 [=36= 的动态 class ]Label因为很难引用CheckBox并分配不同的id和当它们被实例化为 children.
时,它们中的每一个的活动值片段
<Custom>:
CheckBox:
active: False
group: 'search_type'
Label:
size_hint_x: "10"
markup: True
on_ref_press: app.root.setstatus(self)
例子
下面的示例分别为 CheckBox 和 Label 创建两个动态 classes。
main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class Search_type_panel(BoxLayout):
max1_optn = ObjectProperty(None)
max2_optn = ObjectProperty(None)
both_optn = ObjectProperty(None)
def setstatus(self, label):
print("\nsetstatus:")
for key in label.refs:
print("\tUser clicked on refs =", key)
class TestApp(App):
def build(self):
return Search_type_panel()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
<CustomCheckBox@CheckBox>:
active: False
group: 'search_type'
<CustomLabel@Label>:
size_hint_x: "10"
markup: True
on_ref_press: app.root.setstatus(self)
<Search_type_panel>:
max1_optn: max1
max2_optn: max2
both_optn: both
BoxLayout:
padding: "10dp"
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
CustomCheckBox:
id: max1
active: True
CustomLabel:
text: "[ref=max1]max1[/ref]"
CustomCheckBox:
id: max2
CustomLabel:
text: "[ref=max2]max2[/ref]"
CustomCheckBox:
id: both
CustomLabel:
text: "[ref=both]both[/ref]"