2013-04-11 31 views
0

从wxPython开始并获得意外的缩进错误。wxPython意外缩进

File "gui_texteditor_men.py", line 18 
helpMenu= wx.Menu() 
^ 
IndentationError: unexpected indent 

我检查了我的代码,(使用记事本IM ++)和所有缩进都很好,不知道在哪里我已经错了。

#!/usr/bin/python 

import wx 
import os 

class MainWindow(wx.Frame): 
    def __init__(self,parent,title): 
     self.dirname="" 

     wx.Frame.__init__(self, parent, title=title, size =(200,-1)) 
     self.CreateStatusBar() 


     fileMenu= wx.Menu() 
     helpMenu= wx.Menu() 
     menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit") 
     menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program") 
     menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program") 

     # Creating the menubar. 
     menuBar = wx.MenuBar() 
     menuBar.Append(fileMenu,"&File") # Adding the "filemenu" to the MenuBar 
     menuBar.Append(helpMenu, "&Help") 
     self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. 

     #HELP MENU 
     menuAbout = helpMenu.Append(wx.ID_ABOUT, "&About", "About this program") 

     #add events 
     self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen) 
     self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout) 
     self.Bind(wx.EVT_MENU, self.OnExit, menuExit) 


     self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) 
     self.buttons = [] 
      for i in range(0, 6): 
       self.buttons.append(wx.Button(self, -1, "Button &"+str(i))) 
       self.sizer2.Add(self.buttons[i], 1, wx.EXPAND) 

     # Use some sizers to see layout options 
     self.sizer = wx.BoxSizer(wx.VERTICAL) 
     self.sizer.Add(self.control, 1, wx.EXPAND) 
     self.sizer.Add(self.sizer2, 0, wx.EXPAND) 

     #Layout sizers 
     self.SetSizer(self.sizer) 
     self.SetAutoLayout(1) 
     self.sizer.Fit(self) 
     self.Show() 

    def OnAbout(self,e): 
     dlg = wx.MessageDialog(self, "A small text editor", "My test editor", wx.OK) #create a dialog (dlg) box to display the message, and ok button 
     dlg.ShowModal() #show the dialog box, modal means cannot do anything on the program until clicks ok or cancel 
     dlg.Destroy() #destroy the dialog box when its not needed 

    def OnExit(self,e): 
     self.Close(True) #on menu item select, close the app frame. 

    def OnOpen(self,e): 
     self.dirname="" #set directory name to blank 
     dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.*", wx.OPEN) 
     if dlg.ShowModal() == wx.ID_OK: #if positive button selected.... 
      self.filename = dlg.GetFilename() 
      self.dirname = dlg.GetDirectory() 
      f = open(os.path.join(self.dirname, self.filename), 'r') 
      self.control.SetValue(f.read()) #open the file from location as read 
      f.close 
     dlg.Destroy() 

app = wx.App(False) #creates a new app 
frame = MainWindow(None, "Simple Text Editor") #give the frame a title 
app.MainLoop() #start the apps mainloop which handles app events 

检查语法并没有发现任何明显的,任何人都可以吗?

+0

认为当我放入SO时格式化会搞砸。这就是所有的代码 – 2013-04-11 11:33:17

回答

2

您可能正在混合制表符和空格。不要这样做。

要修复您的文件,在混合选项卡,和空间的错误模式下运行的Python:

python -tt yourscript.py 

然后配置你的编辑器来使用缩进空间,并用空格代替任何选项卡。 Python styleguide建议仅使用空格进行标识。

+0

跑了,你建议并得到了错误: 文件 “gui_texteditor_men.py”,第12行 wx.Frame .__的init __(自我,父母,标题=标题,大小=(600,400)) ^ TabError:在缩进中使用制表符和空格不一致 – 2013-04-11 11:32:43

+0

@BenMarks:你走了。使用编辑器中的空格替换制表符,纠正您现在具有的任何不一致的缩进,并将编辑器配置为*仅*使用空格。 – 2013-04-11 11:36:29

+0

是的,刚刚完成,并用空格替换了所有选项卡,再次运行。谢谢 – 2013-04-11 11:37:27