【发布时间】:2011-07-19 23:50:18
【问题描述】:
我最近几天一直在使用我发现的在线教程学习 python,一切都很顺利,直到我遇到一个我似乎无法修复的错误。
我今天使用的网站是http://wiki.wxpython.org/Getting%20Started
我已经顺利完成了大小调整。当我尝试运行教程中的程序时,出现以下错误:
回溯(最近一次通话最后一次):
文件“C:/Python27/test.pyw”,第 4 行,在
类 MainWindow(wx.Frame):
文件“C:/Python27/test.pyw”,第 8 行,在 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()
我已经为此工作了大约一个小时。重新输入并检查了几次。任何以建议或其他教程形式提供的帮助将不胜感激。另外,是否有任何常见错误列表?
【问题讨论】:
-
如果您正在学习一门新语言,请不要从 gui 开始。从简单的控制台应用开始。
-
在我看来,您好像需要从最后三行中删除缩进:
app=wx.App(False)、frame=MainWindow(None,'Sample editor')和app.MainLoop()但是,我无法确认,因为我没有没有安装 wx。