Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, June 8, 2011

About the wxPython: Exit, Destroy, Close


Robin Dunn wrote:

I assume that by "Exit" here you are still talking about the menu item above.

No, I was referring to wx.Exit, which I think is a global call.

It is a way to forcibly kill the app, without any opportunity for the application objects to clean up after themselves. Most of the time you wouldn't want to use it.

You use Close() when you want to programatically tell the frame to go close itself, and is functionally the same as the user telling it to close itself with the "X" button.

Destroy() tells wx to delete the C++ object instance that corresponds to the frame. Normally it will destroy itself when it closes in the default EVT_CLOSE event handler, but if you catch the EVT_CLOSE yourself you either need to call Destroy in your handler, or call event.Skip so the default handler will still run. The EVT_CLOSE handler is where you would normally put the code that checks for open files, asks the user if she wants to save them or cancel, etc. Based on the user's response you can veto the close if you want.

I don't understand. So Close() does nothing more than make the window disappear?

No, it tells the window to close itself. That is a lot different than just hiding it.

Does Close() then automatically call Destroy()?

No, Close causes a EVT_CLOSE event to be sent. The default handler for that event calls Destroy().

What happens if I catch the wx.EVT_CLOSE event and make it call self.frame.Close()?

Then you'll get an endless loop.

Does it get destroyed also, or is the application still running somehow?

According to you and Andrea, I think this is what I need:

self.frame.Bind(wx.EVT_CLOSE, self.OnExitApp)

def OnExitApp(self, event): self.frame.Destroy()

Right? Or do I just use Close() still?

You use Close() from your menu event handler, or wherever you want to programatically cause the frame to be closed. You only need to provide a handler for EVT_CLOSE if you want to have more control over the closing of the frame than the default automatic destroy.

No comments:

Post a Comment