python - 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()
解决方案
你导入错了Label
。改变:
from kivy.core.text import Label
至:
from kivy.uix.label import Label
推荐阅读
- php - 如何求和/获取总价 laravel 上的所有价格
- python-3.x - 定义功能 - 甜甜圈
- javascript - 使用 Vanilla JS 从 ReadableStream 解析 JSON 数据块
- node.js - 遵循 Shopify 的 React 和 Node 教程时出现未处理的运行时错误
- c# - 如何使此名称生成器完全随机
- reactjs - 停止监听更改以存储在 react-redux 中
- javascript - “npm install”不安装任何东西
- javascript - discord.js - 排行榜未定义的用户名
- android - 滚动 RecyclerView 时如何获取更新的滚动位置
- html - 在单选按钮图像上设置悬停图像