首页 > 解决方案 > I am trying to add label in kivy using python file. But it is throwing an error everytime

问题描述

I want a label to be added when I press a button. So I created a function in .py file. But if I press the button it shows "AttributeError: 'LabelSDL2' object has no attribute 'bind' ".

I want the label in the python file. Because I will remove it later.

What should I do?

Python File:

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 file:

MainWidget:

<MainWidget>:

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

标签: pythonkivylabel

解决方案


你导入错了Label。改变:

from kivy.core.text import Label

至:

from kivy.uix.label import Label

推荐阅读