【问题标题】:Python Error(Beginner)Python 错误(初学者)
【发布时间】: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。

标签: python wxpython


【解决方案1】:

wx.Frame.__init__(...) 到但不包括下一个def must be indented one additional level 的所有内容。

【讨论】:

    【解决方案2】:

    您有一个缩进错误:第 8 行以下的所有内容都应缩进以匹配前几行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-20
      • 2015-07-08
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多