【问题标题】:I am trying to add label in kivy using python file. But it is throwing an error everytime我正在尝试使用 python 文件在 kivy 中添加标签。但它每次都抛出错误
【发布时间】:2021-09-30 11:22:39
【问题描述】:

我希望在按下按钮时添加标签。所以我在 .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()

【问题讨论】:

    标签: python kivy label


    【解决方案1】:

    您导入了错误的Label。变化:

    from kivy.core.text import Label
    

    到:

    from kivy.uix.label import Label
    

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2021-12-10
      • 2019-12-03
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 2018-04-06
      相关资源
      最近更新 更多