我正在尝试使用 python 文件在 kivy 中添加标签。但它每次都抛出错误

I am trying to add label in kivy using python file. But it is throwing an error everytime

我想要在按下按钮时添加标签。所以我在 .py 文件中创建了一个函数。但是,如果我按下按钮,它会显示“AttributeError: 'LabelSDL2' object has no attribute 'bind'”。

我想要 python 文件中的标签。因为我稍后会删除它。

我该怎么办?

Python 文件:

from kivy.app import App
from kivy.core.text import Label
from kivy.uix.relativelayout import RelativeLayout


class MainWidget(RelativeLayout):

    def add_label(self):
        label = Label(text="Label Added", pos=(100, 100), size_hint=(.1, .1))

        self.add_widget(label)


class LabelApp(App):
    pass


LabelApp().run()

kv 文件:

MainWidget:

<MainWidget>:

    Button:
        text: "Add Label"
        size_hint: .1, .1
        on_press: root.add_label()

您正在导入错误 Label。变化:

from kivy.core.text import Label

至:

from kivy.uix.label import Label