[wxpython-users] code structure - suggestions? (classes for gui,
data, calculations...)
Gre7g Luterman
gre7g.luterman at gmail.com
Fri May 16 14:34:46 PDT 2008
On 5/16/08, Vlastimil Brom <vlastimil.brom at gmail.com> wrote:
>
> <snipped> What would be an usual way of structuring the code? I tried to
> separate distinct tasks to classes, but I am not sure, if this is the rig=
ht
> way: <snipped>
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200805=
16/7739e650/attachment-0001.htm
More information about the wxpython-users
mailing list