考虑以下代码:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class MyApp(App):
def build(self):
sm.add_widget(InputScreen(name="Input"))
return sm
Builder.load_string('''
<InputScreen>:
BoxLayout:
Label:
id: name_label
text: "x"
''')
class InputScreen(Screen):
def press(self):
name = "Trevor"
self.ids.name_Label.text = "Is your name"+name+" ?" #Error line
if __name__ == '__main__':
MyApp().run()
当我执行代码时,我收到错误 AttributeError: 'super' object has no attribute 'getattr'。您的意思是:“setattr”吗?排队(见评论)。
我处理ID错误了吗?解决办法是什么?
您收到的错误是因为您尝试使用 name_Label 属性访问 name_label 小部件,但您输入了错误。在 Kivy 语言中,ID 区分大小写,因此是 name_label 而不是 name_Label。
self.ids.name_label.text = "Is your name " + name + " ?"