需要解释 Kivy 规则 [关闭]
Need explanation about Kivy Rules [ closed ]
目前我正在学习 Kivy,我已经阅读了 documentation 关于 kv lang 的文章。但我不太了解 kv 规则 :
想问:
- 什么是根规则
- 如何声明根规则以及在哪里
- 可以举一些例子:) ?
喜欢 : 在 .kv 文件中
#:kivy 2.0.0
<main>: ----------------------------------------- <-- class rule
Label: -------------------------------------- <-- root rule
text: "Hello World !" <-- child of Label
font_size: 70 <-- child of Label
center_x: root.width /2 <-- child of Label
center_y: root.height /2 <-- child of Label
BoxLayout: ---------------------------------- <-- root rule
size: root.width /2, root.height /3 <-- child of BoxLayout
Button: <-- child of BoxLayout
text: "Button 1" <-- child of Button
font_size: 30 <-- child of Button
Button: <-- child of BoxLayout
text: "Button 2" <-- child of Button
font_size: 30 <-- child of Button
是这样的吗?
不完全是。 <main>
是根规则,但它不是 root widget
,Label
和 BoxLayout
是名为 [=] 的 class 实例的子项15=]。在 <main>
规则中使用 root
(如 root.xxx
)是对 main
实例的引用。请注意,包含在 <>
中的规则可以被认为是对如何构建 <>
中命名的 class 实例的描述。这样的规则本身不会触发小部件的创建。
文档中没有很好解释的是 kv
中也有可能出现 root widget
。例如:
main:
Label:
text: "Hello World !"
font_size: 70
center_x: root.width /2
center_y: root.height /2
这里main
是一个根规则(因为它没有缩进),也是一个root widget
,因为它没有周围的<>
。只允许一个 root widget
。这通常用于定义应用程序的 root widget
。在 kv
中的任何地方使用 app.root
(不限于上述规则)将引用 root widget
实例。加载此规则时,它将生成 class main
的实例,其中包含规则中描述的子项。
顺便说一句,kv
中引用的 classes 应该大写。 kv
中的 class 名称不大写可能会导致意外错误。
目前我正在学习 Kivy,我已经阅读了 documentation 关于 kv lang 的文章。但我不太了解 kv 规则 :
想问:
- 什么是根规则
- 如何声明根规则以及在哪里
- 可以举一些例子:) ?
喜欢 : 在 .kv 文件中
#:kivy 2.0.0
<main>: ----------------------------------------- <-- class rule
Label: -------------------------------------- <-- root rule
text: "Hello World !" <-- child of Label
font_size: 70 <-- child of Label
center_x: root.width /2 <-- child of Label
center_y: root.height /2 <-- child of Label
BoxLayout: ---------------------------------- <-- root rule
size: root.width /2, root.height /3 <-- child of BoxLayout
Button: <-- child of BoxLayout
text: "Button 1" <-- child of Button
font_size: 30 <-- child of Button
Button: <-- child of BoxLayout
text: "Button 2" <-- child of Button
font_size: 30 <-- child of Button
是这样的吗?
不完全是。 <main>
是根规则,但它不是 root widget
,Label
和 BoxLayout
是名为 [=] 的 class 实例的子项15=]。在 <main>
规则中使用 root
(如 root.xxx
)是对 main
实例的引用。请注意,包含在 <>
中的规则可以被认为是对如何构建 <>
中命名的 class 实例的描述。这样的规则本身不会触发小部件的创建。
文档中没有很好解释的是 kv
中也有可能出现 root widget
。例如:
main:
Label:
text: "Hello World !"
font_size: 70
center_x: root.width /2
center_y: root.height /2
这里main
是一个根规则(因为它没有缩进),也是一个root widget
,因为它没有周围的<>
。只允许一个 root widget
。这通常用于定义应用程序的 root widget
。在 kv
中的任何地方使用 app.root
(不限于上述规则)将引用 root widget
实例。加载此规则时,它将生成 class main
的实例,其中包含规则中描述的子项。
顺便说一句,kv
中引用的 classes 应该大写。 kv
中的 class 名称不大写可能会导致意外错误。