如何为嵌套在另一个小部件中的标签设置文本(无 python 代码)
How to Set Text for Label Nested in Another Widget (no python code)
我正在尝试在 .kv 文件中创建一个通用 header class 并从另一个 class 设置其文本,但我找不到任何关于如何创建的文档做这个。我正在尝试的代码示例是:
NestedLabel.kv:
#:kivy 2.0.0
<MyWidgetOne@Label>:
text: "Widget One"
<MyWidgetTwo@Label>:
text: "Widget Two"
<Header@BoxLayout>:
# class to reuse over multiple views
the_label: id_label
size_hint: 1, None
height: dp(40)
canvas:
Color:
rgba: 1, 0, 0, 1
Rectangle:
size: self.size
pos: self.pos
Label:
id: id_label
text: "---headertext---" # * want to set this generic text
bold: True
<ViewOne>:
BoxLayout:
orientation: "vertical"
Header:
the_label: "ViewOneHeader" # how to set * (above) from here?
# the_label:
# text: "ViewOneHeader" # not working
# the_label.text: "ViewOneHeader" # also not working
# text: "ViewOneHeader" # still not working
MyWidgetOne:
MyWidgetTwo:
NestedLabel.py:
from kivy.app import App
from kivy.uix.screenmanager import Screen
class ViewOne(Screen):
pass
class NestedLabelApp(App):
def build(self):
self.root = root = ViewOne()
return root
NestedLabelApp().run()
截图:
红色的header表示---headertext---
;我正在尝试将其设置为 ViewOneHeader
。感谢任何提示。
您可以在 Header
class 中设置 Property
,方法是添加:
header_text: "default"
到 kv
中的 Header
规则。然后用它来设置Label
文本:
text: root.header_text # * want to set this generic text
然后可以在任何 kv
规则中设置 属性 通过设置创建 Header
:
header_text: "This is the Header"
这是执行上述操作的 kv
的修改版本:
#:kivy 2.0.0
<MyWidgetOne@Label>:
text: "Widget One"
<MyWidgetTwo@Label>:
text: "Widget Two"
<Header@BoxLayout>:
# class to reuse over multiple views
header_text: "default" # creates a property
the_label: id_label
size_hint: 1, None
height: dp(40)
canvas:
Color:
rgba: 1, 0, 0, 1
Rectangle:
size: self.size
pos: self.pos
Label:
id: id_label
text: root.header_text # use the created property here
bold: True
<ViewOne>:
BoxLayout:
orientation: "vertical"
Header:
header_text: "This is the Header" # set the desired value of the new property
MyWidgetOne:
MyWidgetTwo:
我正在尝试在 .kv 文件中创建一个通用 header class 并从另一个 class 设置其文本,但我找不到任何关于如何创建的文档做这个。我正在尝试的代码示例是:
NestedLabel.kv:
#:kivy 2.0.0
<MyWidgetOne@Label>:
text: "Widget One"
<MyWidgetTwo@Label>:
text: "Widget Two"
<Header@BoxLayout>:
# class to reuse over multiple views
the_label: id_label
size_hint: 1, None
height: dp(40)
canvas:
Color:
rgba: 1, 0, 0, 1
Rectangle:
size: self.size
pos: self.pos
Label:
id: id_label
text: "---headertext---" # * want to set this generic text
bold: True
<ViewOne>:
BoxLayout:
orientation: "vertical"
Header:
the_label: "ViewOneHeader" # how to set * (above) from here?
# the_label:
# text: "ViewOneHeader" # not working
# the_label.text: "ViewOneHeader" # also not working
# text: "ViewOneHeader" # still not working
MyWidgetOne:
MyWidgetTwo:
NestedLabel.py:
from kivy.app import App
from kivy.uix.screenmanager import Screen
class ViewOne(Screen):
pass
class NestedLabelApp(App):
def build(self):
self.root = root = ViewOne()
return root
NestedLabelApp().run()
截图:
---headertext---
;我正在尝试将其设置为 ViewOneHeader
。感谢任何提示。
您可以在 Header
class 中设置 Property
,方法是添加:
header_text: "default"
到 kv
中的 Header
规则。然后用它来设置Label
文本:
text: root.header_text # * want to set this generic text
然后可以在任何 kv
规则中设置 属性 通过设置创建 Header
:
header_text: "This is the Header"
这是执行上述操作的 kv
的修改版本:
#:kivy 2.0.0
<MyWidgetOne@Label>:
text: "Widget One"
<MyWidgetTwo@Label>:
text: "Widget Two"
<Header@BoxLayout>:
# class to reuse over multiple views
header_text: "default" # creates a property
the_label: id_label
size_hint: 1, None
height: dp(40)
canvas:
Color:
rgba: 1, 0, 0, 1
Rectangle:
size: self.size
pos: self.pos
Label:
id: id_label
text: root.header_text # use the created property here
bold: True
<ViewOne>:
BoxLayout:
orientation: "vertical"
Header:
header_text: "This is the Header" # set the desired value of the new property
MyWidgetOne:
MyWidgetTwo: