【发布时间】:2021-05-06 20:14:24
【问题描述】:
我是 python 初学者。 我喜欢了解 Kivy 和 Python 的概念。 我不明白为什么下面的代码会出错。
Python 文件
from kivy.app import App
from kivy.uix.button import Button
class MyButton(Button):
def __init__(self, **kwargs):
super(MyButton, self).__init__(**kwargs)
print(f"self.name is{self.name}") #it makes error.
def print_name(self):
print(self.name) #But it doesn't make error!!
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
self.title = 'Test'
if __name__ == '__main__':
TestApp().run()
Kivy 文件
#:kivy 1.10.0
<MyButton>:
name: "MyButton"
on_press: self.print_name()
MyButton:
text: self.name
我认为 name 属性已经存在,因为它是作为 kivy 文件中的类属性创建的,因此 print_name 方法不会出错。
句子中哪里有错误? 如何在 init 方法中使用 kivy-file 中的名称属性(属性)?
【问题讨论】:
标签: python properties attributes kivy