开发者

Terminating a wxPython app cleanly

I'm working on a wxPython app which has multiple frames and a serial connection. I need to开发者_如何学JAVA be able to exit the app cleanly, closing the serial connection as the app terminates.

What is the best way to do this? Should this be handled by a subclass of wxApp?

Thanks, Josh


Not totally sure what you're having trouble with here, but I'll take a shot in the dark and assume this is a program organization/design question.

There may be better ways that I'm unfamiliar with, but this is what I'd try to do: create a "parent" object (doesn't have to have any particular type) that keeps references to the multiple frames. When one of the frames receives the exit event from a menu or what have you, it calls self.parent.broadcast_quit(), which sends a quit event to each of the frames it holds references to.

This way of doing it kind of expresses a view-controller separation, where the frames are part of the view and the parent object is a tell-me-when-to-shut-down controller -- the parent can nicely encapsulate the connection teardown as well, since it can stay informed on which frames have shut down and when. You can keep details on how to tear down the view in the frames themselves, and then can send an "I'm finished" message back to the controller during their teardown, triggering some final teardown on the parent side.

See the docs for wx.PostEvent and Custom Event Classes. (Events are a nice normalized way of expressing cross-window messages in a non-blocking GUI.)


I usually use my close button's event handler to close connections and what-not before closing the frame. If you want to catch the upper right "x" button, then you'll need to bind to EVT_CLOSE. Unfortunately, when you do that, you need to call your frame's Destroy() method rather than its Close() method or you'll end up in an infinite loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜