我试图学习如何使用我的文本编辑器打开文件,或者至少先找到它们。但是当我运行时(使用wxPython)我得到这个错误:
def OnOpen(self,e):
^
IndentationError:unindent与任何外部缩进级别都不匹配
import wx
import sys
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self,parent,title=title,size=(500,300))
self.Control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar()
filemenu = wx.Menu()
Open = filemenu.Append(wx.ID_OPEN, "Open", "Opens a file.")
Save = filemenu.Append(wx.ID_SAVE, "Save", "Saves a file.")
Close = filemenu.Append(wx.ID_CLOSE, "Close", "Close a file.")
infomenu = wx.Menu()
About = infomenu.Append(wx.ID_ABOUT, "About", "Info about the program.")
infomenu.AppendSeparator()
Exit = infomenu.Append(wx.ID_EXIT, "Exit", "Exits the program!")
menuBar = wx.MenuBar()
menuBar.Append(filemenu, "File")
menuBar.Append(infomenu, "Info")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnExit, Exit)
self.Bind(wx.EVT_MENU, self.OnOpen, Open)
self.Show(True)
def OnExit(self, event):
exit()
def OnOpen(self,e):
""" Open a file"""
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()
app = wx.App(False)
frame = MyFrame(None, "Hrrs01's text editor")
app.MainLoop()
答案 0 :(得分:0)
您在同一文件中混合了制表符和空格,但将制表符大小设置为4个单元格。使用python -tt
进行确认。