-1

有人能看看这段代码有什么问题吗?一切对我来说都很好,但我得到了缩进错误。

下面的代码创建了一个新框架并包含一个按钮。当单击该按钮时,将触发一个事件并显示有关父项和子项的更多信息。

代码:

import wx
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title="The Main Frame")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MyFrame(wx.Frame):
    def __init__(self, parent, id=wx.ID_ANY, title="", 
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=wx.DEFAULT_FRAME_STYLE,
                 name="MyFrame"):
        super(MyFrame, self).__init__(parent, id, title,
                                      pos, size, style, name)
        # Attributes
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(wx.BLACK)
        self.button = wx.Button(self.panel,
                                label="Push Me",
                                pos=(50, 50))
        self.btnId = button.GetId()
        # Event Handlers
        self.Bind(wx.EVT_BUTTON, self.OnButton, button)

    def OnButton(self, event):
        """Called when the Button is clicked"""
        print "\nFrame GetChildren:"
        for child in self.GetChildren():
            print "%s" % repr(child)
        print "\nPanel FindWindowById:"
        button = self.panel.FindWindowById(self.btnId)
        print "%s" % repr(button)
        # Change the Button's label
        button.SetLabel("Changed Label")
        print "\nButton GetParent:"
        panel = button.GetParent()
        print "%s" % repr(panel)
        print "\nGet the Application Object:"
        app = wx.GetApp()
        print "%s" % repr(app)
        print "\nGet the Frame from the App:"
        frame = app.GetTopWindow()
        print "%s" % repr(frame)

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()  

这是错误:

C:\Python27\wx>python frame_new.py
  File "frame_new.py", line 22
    self.btnId = button.GetId()
    ^
IndentationError: unexpected indent                         
4

1 回答 1

2

在某些可以显示制表符和空格的程序中打开该文件,例如 Windows 上的 Notepad++。然后你可以看看你是否有某种标签/空格组合或其他东西。

于 2012-05-16T20:47:59.390 回答