#/usr/bin/python#-*-<coding=UTF-8>-*-"""此樣本主要向大家展示如何在一個架構中添加對象;wxPython 2.6章節樣本講解"""import wxclass InsertFrame(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,title="Frame with button",size=(300,100)) #在wx.Frame中建立一個panel控制項 panel = wx.Panel(self) #在wx.Panel中建立一個Button控制項 button = wx.Button(panel,label="close",pos=(125,10),size=(50,50)) #綁定按鈕的單擊事件,注意是將wx.Frame綁定到OnCloseMe事件上,而不是其它對象 self.Bind(wx.EVT_BUTTON,self.OnCloseMe,button) #綁定視窗的關閉事件 self.Bind(wx.EVT_CLOSE,self.OnCloseWindow) def OnCloseMe(self,event): #close方法是從哪裡來的,看了一下API,wx.Frame好像沒有Close()方法? self.Close(True) print self.Close.__doc__ """ Close(self, bool force=False) -> bool This function simply generates a EVT_CLOSE event whose handler usually tries to close the window. It doesn't close the window itself, however. If force is False (the default) then the window's close handler will be allowed to veto the destruction of the window. """ def OnCloseWindow(self,event): #Destroy()方法只有wx.App才有,為什麼wx.Frame也可以調用,此處不明白? self.Destroy() print self.Destroy.__doc__ """ Destroy(self) -> bool Destroys the window safely. Frames and dialogs are not destroyed immediately when this function is called -- they are added to a list of windows to be deleted on idle time, when all the window's events have been processed. This prevents problems with events being sent to non-existent windows. Returns True if the window has either been successfully deleted, or it has been added to the list of windows pending real deletion. """ if __name__ == "__main__": app = wx.PySimpleApp() frame = InsertFrame(parent=None,id=-1) frame.Show() app.MainLoop()