Why am I getting a (NameError: name 'self' is not defined ) here using Kivy for Python?

Why am I getting a (NameError: name 'self' is not defined ) here using Kivy for Python?

我正在制作游戏,正在研究屏幕切换。我希望在按下 "play" 按钮时切换屏幕,但我得到的是 NameError。这是代码:

class menu_screen(Screen):
    def __init__(self, **kwargs):
        super (menu_screen,self).__init__(**kwargs)

        menu_layout = FloatLayout
        logo_label = Label(text = "GAME", size_hint=(.6, .2),pos_hint={'x':.2, 'y':.65})
        play_button = Button(text = "Play", size_hint=(.6, .1),pos_hint={'x':.2, 'y':.5})
        play_button.bind(on_release = self.mainscreen_change)
        about_button = Button(text = "About", size_hint=(.6, .1),pos_hint={'x':.2, 'y':.35})
        settings_button = Button(text = "Settings", size_hint=(.6, .1),pos_hint={'x':.2, 'y':.2})
        dev_label = Label(text = "Developer: FRG100", size_hint=(.1, .15),pos_hint={'x':.45, 'y':.9})

        menu_layout.add_widget(logo_label)
        menu_layout.add_widget(dev_label)
        menu_layout.add_widget(play_button)
        menu_layout.add_widget(about_button)
        menu_layout.add_widget(settings_button)
        self.add_widget(menu_layout)

    def mainscreen_change(self, *args):
        my_screenmanager.current = 'mainscreen'

我收到 NameError: name 'self' is not defined for this line:

 play_button.bind(on_release = self.mainscreen_change)

感谢您的帮助!

您混用了制表符和空格:

当您这样做时,Python 会对应该如何缩进感到困惑,因为它不会像您的编辑器那样解释制表符。不要混用制表符和空格;坚持其中一个,并在您的编辑器中打开 "show whitespace",这样这些问题就会清晰可见。