This sort of thing is being asked a lot lately. Here’s a reply I submitted to an earlier thread. Hopefully it will help. Oh and also, if you must reply to a digest, kindly remove all the text.
I posted a similar question before beginning my current project. Someone (forgot who, sorry I didn’t give credit) wisely suggested following the Model/View/Controller pattern, which I did, and I’m pretty pleased with how things worked out.
I created three subdirectories under my main program directory which I called VIEW, MDL, and CTRL. In each of these, I put a init.py file which instantiated a Viewer, Model, and Controller instance, respectively. From there, I put all GUI calls in my VIEW.Viewer object, all event handlers in my CTRL.Controller object, and all my application-specific stuff under MDL.Model. All coding in VIEW and CTRL presume they will run in the main thread only* and all code in MDL must be kept threadsafe. If a callback function has to be passed to a member in MDL, I alway refer to a CTRL.Controller.TS_XXX function which ONLY launches a wx.CallAfter to return to the main thread.
I had a few things that didn’t fit the model precisely. I put some items (such as my wx.App) in the top-level directory since they didn’t seem to go well anywhere else. Also, I had some low-level handlers which were basically self-contained, so I left them in the window class that they belonged to, but put a lot of comments around them to explain why I had deviated from the MVC model.
I will admit that following this model created a few extra levels of calls for me. For instance, when the controller wants to call a method belonging to an object in the viewer, it would instead call a function in the viewer and that function was basically just a call to the specific object’s method. As a hand-optimizer, this reallly grated on me, but it has already saved me a few times, so I grin and bear it.
I don’t know that I followed the pattern well. This was my first MVC attempt, so I may have botched it completely, but it’s working well for me and I’m pleased with how smoothly it’s gone.
Since the details are pretty OT, feel free to e-mail me directly if you have any specific questions for how I did my layout. I will definitely follow this pattern again next time unless someone else has a suggestion of how I should refine this. Is this how you guys do your programs as well? I’d love to get other tips.
Gre7g
···
On 6/5/08, Sanne Korzec sanne@kortec.nl wrote:
I’m writing a Wxpython program with the following classes:
A wxpython GUI class (or app if you will) which handles all my events and a
separate class with some methods…