从wxPython开始并出现意外的缩进错误。
File "gui_texteditor_men.py", line 18
helpMenu= wx.Menu()
^
IndentationError: unexpected indent
我已经检查了我的代码,(我使用记事本++)并且所有的缩进都很好,不知道我哪里出错了。
#!/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 :(得分:2)
您可能正在混合制表符和空格。不要那样做。
要修复文件,请在混合制表符和空格错误模式下运行Python:
python -tt yourscript.py
然后将编辑器配置为仅使用 空格进行缩进,并用空格替换所有选项卡。 Python styleguide建议仅使用空格进行标识。