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

1.5k views Asked by At

I'm making a game and I'm working on the screen switching. I want the screen to switch when I press the "play" button but I get a NameError instead. Here's the code:

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'

I get a NameError: name 'self' is not defined for this line:

 play_button.bind(on_release = self.mainscreen_change)

Thank you for your help!

1

There are 1 answers

0
user2357112 On BEST ANSWER

You've mixed tabs and spaces:

picture of tabs and spaces

When you do that, Python gets confused about how things are supposed to be indented, because it doesn't interpret tabs the same way your editor does. Don't mix tabs and spaces; stick to one or the other, and turn on "show whitespace" in your editor so these problems are clearly visible.