[英]Python Error(Beginner)
我在最后一天左右使用我发现的在线教程学习 python,一切都进展顺利,直到我遇到一个我似乎无法修复的错误。
我今天使用的站点是http://wiki.wxpython.org/Getting%20Started
我已经很轻松地完成了大小调整。 当我尝试运行教程中的程序时,出现以下错误:
回溯(最近一次通话最后):
文件“C:/Python27/test.pyw”,第 4 行,在
class 主窗口(wx.Frame):
MainWindow 中的文件“C:/Python27/test.pyw”,第 8 行
wx.Frame. init (self,parent,title=title,size=(200,-1))
NameError:名称“自我”未定义
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.control=wx.TextCtrl(self,style=wx.TE_MULTILINE)
self.CreateStatusBar()
filemenu=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")
menuBar=wx.MenuBar()
menuBar.Append(filemenu,'&File')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU,self.OnOpen,menuOpen)
self.Bind(wx.EVT_MENU,self.OnExit,menuExit)
self.Bind(wx.EVT_MENU.self.OnAbout,menuAbout)
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)
self.SetSizers(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show()
def OnAbout(self,e):
dlg=wx.MessageDialog(self,'A sample editor \n in wxPython', 'About sample editor', wx.OK)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
def OnOpen(self,e):
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=MainWindow(None,'Sample editor')
app.MainLoop()
我已经为此工作了大约一个小时。 重新输入并检查了几次。 任何以建议或其他教程形式提供的帮助将不胜感激。 此外,是否有任何常见错误列表?
从wx.Frame.__init__(...)
到但不包括下一个def
的所有内容都必须缩进一层。
你有一个缩进错误:从第 8 行开始的所有内容都应该缩进以匹配前面的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.