【问题标题】:Need explanation about Kivy Rules [ closed ]需要关于 Kivy 规则的解释 [关闭]
【发布时间】:2021-04-19 06:56:00
【问题描述】:

目前我正在学习 Kivy,并且我已经阅读了关于 kv lang 的 documentation。但我不太了解 kv 规则:

想问:

  • 什么是根规则
  • 如何声明根规则以及在哪里声明
  • 可以举一些例子:)?

like : 在 .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

是这样的吗?

【问题讨论】:

    标签: python kivy rules


    【解决方案1】:

    不完全是。 &lt;main&gt; 是根规则,但它不是 root widgetLabelBoxLayout 是名为 main 的类实例的子级。在&lt;main&gt; 规则中对root(如root.xxx)的任何使用都是对main 实例的引用。请注意,&lt;&gt; 中包含的规则可以被认为是对如何构建在&lt;&gt; 中命名的类的实例的描述。这样的规则本身不会触发小部件的创建。

    文档中没有很好解释的是kv 中也有可能出现root widget。例如:

    main:
        Label:
            text: "Hello World !"
            font_size: 70
            center_x: root.width /2
            center_y: root.height /2
    

    这里main是一个根规则(因为它没有缩进),它也是一个root widget,因为它没有包围&lt;&gt;。只允许使用一个root widget。这通常用于定义应用程序的root widget。在kv 的任何地方使用app.root(不限于上述规则内)将引用root widget 实例。加载此规则时,它将生成一个 main 类的实例,其中包含规则中所述的子级。

    顺便说一句,kv 中引用的类应该大写。 kv 中的类名不大写可能会导致意外错误。

    【讨论】:

    • oic ~ 我现在对 kivy 规则的工作原理有了更好的理解 :) 感谢您的解释 :)
    • 除此之外,想问一下canvas?真的很想知道它是如何工作的:(
    • 这里不是开kivy课程的地方,请以this开头。
    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多