SyntaxError "no viable alternative at input 'self'"

35,076

You forgot to close the parens on the previous line of code.

Share:
35,076
Timothy
Author by

Timothy

Updated on July 28, 2022

Comments

  • Timothy
    Timothy almost 2 years

    I have a gui.py file containing the following code:

    from javax.swing import JFrame, JPanel, Box, JComboBox, JSpinner, JButton, JLabel, SpinnerNumberModel, WindowConstants
    from java.awt import BoxLayout, GridLayout
    
    class SettingsWindow:
    
        def start( self ):
            selected = self.combobox.selectedIndex
            if selected >= 0:
                self.map = self.map_list[ selected ]
            self.games = self.spinner.getValue()
    
        def __init__( self, map_list ):
            frame = JFrame( "Settings" )
            frame.setSize( 200, 250 )
            frame.setLayout( BoxLayout() )
            panel = JPanel( GridLayout( 3, 1 )
    
            # Map Combobox
            self.map_list = map_list
            self.combobox = JComboBox( self.map_list )
            map_box = Box( BoxLayout.X_AXIS )
            map_box.add( JLabel( "Select map file:" ) )
            map_box.add( Box.createHorizontalStrut( 15 ) )
            map_box.add( self.combobox )
            panel.add( map_box )
    
            # Games Spinner
            self.spinner = JSpinner( SpinnerNumberModel( 1, 1, 25, 1 ) )
            games_box = Box( BoxLayout.X_AXIS )
            games_box.add( JLabel( "Number of games:" ) )
            map_box.add( Box.createHorizontalStrut( 15 ) )
            games_box.add( self.spinner )
            panel.add( games_box )
    
            # Start Button
            btn = JButton( "Start", actionPerformed = self.start )
            btn_box = Box( BoxLayout.X_AXIS )
            btn_box.add( btn )
            panel.add( btn_box )
    
            frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE )
            frame.setVisible( True )
    
        if __name__ == '__main__':
            SettingsWindow()
    

    Then, in my main file, I call the class above with this code:

    settings = gui.SettingsWindow( map_list )
    

    And I get the error:

    SyntaxError ( ("no viable alternative at input 'self'",   ('.../gui.py', 19, 8, '        self.map_list = map_list\n')) )
    

    If anyone can see what I'm missing, I'd be really grateful for the help!