Python SyntaxError“在输入'self'处没有可行的替代方案”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24857676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:21:32 来源:igfitidea点击:
SyntaxError "no viable alternative at input 'self'"
提问by Timothy
I have a gui.py file containing the following code:
我有一个包含以下代码的 gui.py 文件:
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!
如果有人能看到我缺少的东西,我将非常感谢您的帮助!
采纳答案by Ignacio Vazquez-Abrams
You forgot to close the parens on the previous line of code.
您忘记关闭上一行代码中的括号。