1

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?

2
  • 1
    If you're learning a new language, just don't start with gui. Start with simple console apps. Commented Jul 19, 2011 at 23:58
  • It looks to me as if you need to remove the indentation from your last three lines: app=wx.App(False), frame=MainWindow(None,'Sample editor'), and app.MainLoop() However, I can't confirm, as I don't have wx installed. Commented Jul 20, 2011 at 3:34

2 Answers 2

3

Everything from wx.Frame.__init__(...) up to but not including the next def must be indented one additional level.

Sign up to request clarification or add additional context in comments.

Comments

0

You have an indentation error: everything from the 8th line down should be indented to match the previous lines.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.