Python 错误(初学者)
我在最后一天左右使用我发现的在线教程学习Python,一切都进展顺利,直到出现一个我似乎无法修复的错误。
我今天使用的网站是 http://wiki.wxpython.org/Getting%20Started
我毫无困难地一路到达尺寸测定器。当我尝试运行教程中的程序时,出现以下错误:
回溯(最近一次调用最后一次):
文件“C:/Python27/test.pyw”,第 4 行,位于
类MainWindow(wx.Frame):
主窗口中的文件“C:/Python27/test.pyw”,第 8 行
wx.Frame.init(self,parent,title=title,size=(200,-1))
NameError:名称“self”未定义
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()
我已经为此工作了大约一个小时。重新输入并检查了多次。任何建议或其他教程形式的帮助将不胜感激。另外,是否有任何地方的常见错误列表?
I been learning python the last day or so using online tutorials I found, everything has been coming along well till I got an error that I can not seem to fix.
The site I been using today is http://wiki.wxpython.org/Getting%20Started
I have got all the way to sizers with little trouble. When I try to run a program from the tutorial I get the following error:
Traceback (most recent call last):
File "C:/Python27/test.pyw", line 4, in
class MainWindow(wx.Frame):
File "C:/Python27/test.pyw", line 8, in MainWindow
wx.Frame.init(self,parent,title=title,size=(200,-1))
NameError: name 'self' is not defined
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()
I have been working at this for about a hour. Re-typed and checked over several times. Any help in the form of advice or other tutorials would be greatly appreciated. Also, is there any list of common errors anywhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从
wx.Frame.__init__(...)
到但不包括下一个def
必须再缩进一级。Everything from
wx.Frame.__init__(...)
up to but not including the nextdef
must be indented one additional level.您遇到缩进错误:从第 8 行开始的所有内容都应该缩进以匹配前面的行。
You have an indentation error: everything from the 8th line down should be indented to match the previous lines.