python - Kivy - 如何找出 .kv 文件中 Widget 的路径

标签 python kivy

在我的 python 代码中,我能够使用 self.canvas.before 在 .kv 文件中定义的 Widget 中画一条线。

然后,在 .kv 中,我将 Widget 移至 TabbedPanelItem 内,但它不再正常工作。

self.canvas.before 不是正确的路径,我应该使用什么代替?正确的路径是什么?

一般来说,我如何找出层次结构? .kv 文件中的那些对象是由构建器创建的(如果我的理解是正确的),我如何弄清楚如何将 python 代码绑定(bind)到构建器创建的对象。 感谢您的帮助。

main.py:

class SampBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super(SampBoxLayout, self).__init__(**kwargs)
        with self.canvas.before:
            self.myline=Line(points=(100,100,400,500), close=False, width=2)

main.kv

SampBoxLayout:
<SampBoxLayout>:
    orientation: "vertical"
    padding: 0
    spacing: 0
    TabbedPanel:
        do_default_tab: False
        TabbedPanelItem:
            text: "noc_clk"
            BoxLayout:
                orientation: "vertical"
                Widget:
                    height: "440dp"
                    size_hint_y: None
                    canvas:
                        Color:
                            rgba: 0, 0, 0, 0.5
                TabbedPanel:
                    do_default_tab: False
                    TabbedPanelItem:
                        text: "Node0"
                        BoxLayout:
                            orientation: "horizontal"

最佳答案

实际上,即使添加 kv 文件后,您的应用程序仍然可以正常运行。该线正在 SampBoxLayout 的 Canvas 上绘制,但它不可见,因为每个小部件的不透明度为 1.0,即不透明。在下面的示例中,为了显示绘制的线条,我在 TabbedPanel 下添加了 opacity = 0.5

层次结构/路径

Kv language » Rule context

There are three keywords specific to Kv language:

app: always refers to the instance of your application.

root: refers to the base widget/template in the current rule

self: always refer to the current widget

Value Expressions, on_property Expressions, ids, and Reserved Keywords

self

The keyword self references the “current widget instance”:

Button:
    text: 'My state is %s' % self.state

root

This keyword is available only in rule definitions and represents the root widget of the rule (the first instance of the rule):

<MyWidget>:
    custom: 'Hello world'
    Button:
        text: root.custom

app

This keyword always refers to your app instance. It’s equivalent to a call to kivy.app.App.get_running_app() in Python.

Label:
    text: app.name

Binding Python code to object created in kv file

方法1

  • 在类级别声明一个 ObjectProperty 并将其连接到 kv 文件中创建的对象的 id。这是最佳实践方法。

main.py
from kivy.properties import ObjectProperty


class SampBoxLayout(BoxLayout):
    tp = ObjectProperty(None)

main.kv

<SampBoxLayout>:
    tp: tp
    orientation: "vertical"
    padding: 0
    spacing: 0

    TabbedPanel:
        id: tp
        opacity: 0.5

方法2

  • id 添加到 kv 文件中创建的对象并使用 self.ids.id-nameself.ids['id-name']

最佳实践和速度

Note

Although the self.ids method is very concise, it is generally regarded as ‘best practice’ to use the ObjectProperty. This creates a direct reference, provides faster access and is more explicit.

示例

main.py
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Line
from kivy.lang import Builder


class SampBoxLayout(BoxLayout):
    def __init__(self, **kwargs):
        super(SampBoxLayout, self).__init__(**kwargs)
        with self.canvas.before:
            self.myline=Line(points=(100, 100, 400, 500), close=False, width=2)


if __name__ == "__main__":
    runTouchApp(Builder.load_file('main.kv'))

main.kv

SampBoxLayout:
<SampBoxLayout>:
    orientation: "vertical"
    padding: 0
    spacing: 0

    TabbedPanel:
        opacity: 0.5
        do_default_tab: False

        TabbedPanelItem:
            text: "noc_clk"
            BoxLayout:
                orientation: "vertical"
                Widget:
                    height: "440dp"
                    size_hint_y: None
                    canvas:
                        Color:
                            rgba: 0, 0, 0, 0.5
                TabbedPanel:
                    do_default_tab: False
                    TabbedPanelItem:
                        text: "Node0"
                        BoxLayout:
                            orientation: "horizontal"

输出

Line Drawn

关于python - Kivy - 如何找出 .kv 文件中 Widget 的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136666/

相关文章:

python - Linux环境下的pyqt4

python - 在python中合并字典列表

python - Kivy:标签有另一个位置,然后是矩形

python - 解除绑定(bind)Window.grab_mouse

python - Lightfm : handling user and item cold-start

python - 不带换行符打印 (print 'a' ,) 打印一个空格,如何删除?

python - 关于kivy中事件调度行为的问题

linux - 鼠标事件 Ubuntu 伴侣的奇怪 Kivy 行为

python - 在黑色背景中显示一些文本

python - 将 Widget 添加到 kv 动态类中继承类的子级