After looking through the wiki, I found this
http://wiki.wxpython.org/SeparateGuiAndLogic
There is saw a short wx program that contains something I've never seen before.
In this class
class AppFrame( wx.Frame ) :
there is a line which instantiates a class to handle the gui events
# Must call before any event handler is referenced.
self.eventsHandler = EventsHandler( self )
I like this idea of separating the event handlers into their own
class.
Is this considered a good way to structure a wx program?
Are there any pitfalls with this architecture?
Thanks
--
To unsubscribe, send email to wxPython-user...@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
The only separating it does is move the code to a new class. It does
not reduce coupling at all as the EventsHandler still needs to know
about the structure and nature of the UI. In a true separation of the
GUI and logic in the OOP sense the program logic would not need to know
anything about the UI at all, perhaps other than there are certain
capabilities that can be triggered by sending messages to it. Pubsub or
similar tools are helpful for implementing things like that.
--
Robin Dunn
Software Craftsman
http://wxPython.org
On 9/19/11 4:45 PM, Tony Cappellini wrote:
It's been a while since I used wx, so I wanted to refresh my memory
on pubsub and mvc architecture.
After looking through the wiki, I found this
http://wiki.wxpython.org/SeparateGuiAndLogic
...
Is this considered a good way to structure a wx program?
The only separating it does is move the code to a new class. It does not reduce coupling at all as the EventsHandler still needs to know about the structure and nature of the UI. In a true separation of the GUI and logic in the OOP sense the program logic would not need to know anything about the UI at all, perhaps other than there are certain capabilities that can be triggered by sending messages to it. Pubsub or similar tools are helpful for implementing things like that.
On Sep 21, 2011, at 8:55 AM, Robin Dunn wrote:
> On 9/19/11 4:45 PM, Tony Cappellini wrote:
>> It's been a while since I used wx, so I wanted to refresh my memory
>> on pubsub and mvc architecture.
>>
>> After looking through the wiki, I found this
>>
>>
>> http://wiki.wxpython.org/SeparateGuiAndLogic
>>
>> There is saw a short wx program that contains something I've never seen before.
>>
>> In this class
>> class AppFrame( wx.Frame ) :
>>
>> there is a line which instantiates a class to handle the gui events
>> # Must call before any event handler is referenced.
>>
>> self.eventsHandler = EventsHandler( self )
>>
>>
>> I like this idea of separating the event handlers into their own
>> class.
>>
>> Is this considered a good way to structure a wx program?
>
> The only separating it does is move the code to a new class. It does not reduce coupling at all as the EventsHandler still needs to know about the structure and nature of the UI.
MVC is about separation of concerns though, not reducing coupling throughout all code. In MVC, the controller (in this example, EventsHandler) is coupled to both the view and the model. That is, it knows the structure and nature of both of them. The important part, though, is that the view doesn't know the structure and nature of the model, and vice-versa.
Technically, the above example still does break that by assigning the controller by subclassing wx.Frame (so AppFrame knows and references the controller), but it could be alternatively done as follows, in someplace such as OnInit:
self.frame = wx.Frame(...)
self.eventsHandler = EventsHandler(self.frame)
In any case, moving your event handlers into a separate class definitely moves the code towards better MVC compliance. If you seriously doubt this, try it for a while and see. I think the real reason this approach feels wrong and doesn't jive with a lot of wx developers, is that it is anti-thema to wx design patterns, which have you putting tons of code in wx.Frame subclasses. Make no mistake, though, those wx.Frame subclasses really just handle sub-control initialization and handling of control events to manipulate GUI state, data structures, etc. It rarely ever is used to add new behaviors and capabilities to wx.Frame itself. When you think about the original design and purpose of subclassing, it was probably not to have classes like 'CarThatTakesMeToWork' as a subclass of Car that contains the logic of driving the roads to and from work. ;-) But that is, in practice, the sorts of classes most wx apps write.
> In a true separation of the GUI and logic in the OOP sense the program logic would not need to know anything about the UI at all, perhaps other than there are certain capabilities that can be triggered by sending messages to it. Pubsub or similar tools are helpful for implementing things like that.
Pubsub can help with separation of concerns, but this is not a given - the big question is, where are you putting your signal handlers? If your pubsub signal is sent to the GUI (e.g. wxFrame subclass), and then in that GUI object you're storing and manipulating app data, then you're still not truly separating concerns. Case in point, the PubSub example recently posted in this thread does exactly that.
Regards,
Kevin
>
> --
> Robin Dunn
> Software Craftsman
> http://wxPython.org
>
On Sep 23, 2011, at 9:02 AM, Kevin Ollivier wrote:
> Hi Robin,
>
> On Sep 21, 2011, at 8:55 AM, Robin Dunn wrote:
>
>> On 9/19/11 4:45 PM, Tony Cappellini wrote:
>>> It's been a while since I used wx, so I wanted to refresh my memory
>>> on pubsub and mvc architecture.
>>>
>>> After looking through the wiki, I found this
>>>
>>>
>>> http://wiki.wxpython.org/SeparateGuiAndLogic
>>>
>>> There is saw a short wx program that contains something I've never seen before.
>>>
>>> In this class
>>> class AppFrame( wx.Frame ) :
>>>
>>> there is a line which instantiates a class to handle the gui events
>>> # Must call before any event handler is referenced.
>>>
>>> self.eventsHandler = EventsHandler( self )
>>>
>>>
>>> I like this idea of separating the event handlers into their own
>>> class.
>>>
>>> Is this considered a good way to structure a wx program?
>>
>> The only separating it does is move the code to a new class. It does not reduce coupling at all as the EventsHandler still needs to know about the structure and nature of the UI.
>
> MVC is about separation of concerns though, not reducing coupling throughout all code. In MVC, the controller (in this example, EventsHandler) is coupled to both the view and the model. That is, it knows the structure and nature of both of them. The important part, though, is that the view doesn't know the structure and nature of the model, and vice-versa.
>
> Technically, the above example still does break that by assigning the controller by subclassing wx.Frame (so AppFrame knows and references the controller), but it could be alternatively done as follows, in someplace such as OnInit:
>
> self.frame = wx.Frame(...)
> self.eventsHandler = EventsHandler(self.frame)
>
> In any case, moving your event handlers into a separate class definitely moves the code towards better MVC compliance. If you seriously doubt this, try it for a while and see.
BTW, sorry about how this sounds, it didn't really come out right. I do, however, feel this approach has more advantages than perhaps appear at first glance. I have in my own experience used the EventsHandler approach to separate code into reusable behaviors. (e.g. I once wrote a InlineTextSearchHandler which lets me pass in a searchCtrl and a wx.STC or wx.TextCtrl, and it provides inline search capabilities. With a little work, it could be extended for wx.RichTextCtrl, wx.WebView, etc.) So I know from experience the general approach does work and can be quite useful. You could do this sort of thing with a mixin too, but I personally find this approach to be cleaner, though perhaps that is just bikeshedding. Also, as a general practice, it decreases the amount of work needed to move from code specific to one purpose to reusable code. Some code you won't reuse, but often you don't know these things in advance, and having it 'ready to reuse' when you do need it is icing on the cake. :) It tends to lead to a Unix tool-like approach of building small, focused code modules instead of "just do everything in your wx.Frame subclass". If you've ever seen a 2000 line wx.Frame subclass (I have, and I've even written them! ;-), you can sort of see how this gets out of control.
This is actually where a lot of the raving about 'magic' in Cocoa comes from. It's not that you can do anything special with Obj-C that you can't do with other tools / languages, it's just that you find that this sort of approach of segmenting out behaviors into discrete chunks leads over time to being able to build apps more like "legos" than writing tedious code over and over. You actually get encouraged to be a bit more ambitious with your interfaces and such, because you will code it in a reusable way and start easily throwing advanced behaviors into all your controls quickly.
You can do that sort of thing with wx, but the default design patterns don't really encourage it. So adding reusability is an added upfront effort (much like you must layout your dialogs before you actually build them with sizers) - and that doesn't really jive with the modern, real world nature of coding where requirements are changing all the time and you need to provide quick turnaround. You may suddenly and quickly need reusability when you didn't before. To use a cliched (but perhaps apt) buzzword, it makes code more 'agile'.
Regards,
Kevin
On Sun, Sep 25, 2011 at 8:33 AM, Dev Player <devp...@gmail.com> wrote:
> I thought I saw the MVP OOP pattern demo'd for wxPython to seperate object
> "interface" (ie event handlers). Although it's called MVP it was more like
> MVIP (an ancronmy I made up to show the way I saw the model demo'd) and the
> demo clumped the interface in with either the view or the presentation part
> of the pattern.
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
The other problem I have as an inexperienced OOP and Python programmer is what form the reactor/loops take; meaning do I make one reactor a process and one a thread, use 2 seperate processes and IPC or the broader RPC or go lower level and use just sockets, or whatever you get the idea. I know that wxPython needs to be the primary process. But if I wanted to replace that GUI package, would the next GUI package need to take a different place in the looping parent/child structure. Again.. too much thinking, (ie back to the wx.Frame that does it all).
I needed none of these questions answered if I didn't mind programming in however the situation called for for each script/app whatever. But if I wanted to develope apps with reusable interfaces, and standarize my tools it seemed prudent to at least look into all this.I can also ponder these thoughts because at this time I program Python on as purely reacreation exercise. Maybe these words will actually produce some useful insight.
Ideally, we should all STOP and think just as frequently as we type.
Blend strategy and tactics together.
I don't do that and my code ends up being crappy most of the time.
I sin and I'm punished by my sins.
Anyway, if my intuition is correct, even if you failed in your attempt
with the redesign of the Demo, you've learned a lot about code
structure.
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en
--
If I would have to describe the difference between MVC and MVP, I
would point to the effort to make the View code as dumb as possible.
The Controller code is broken in two: Interactor and Presenter where
the Interactor provides insulation of the Presenter from the View.
If done right, the Interactor and the View are dumb, dumb, dumb and
are the only zones where something like wxPython appears.
Being so dumb minimizes failure in these components. It also means
that another toolkit could be used (theoretically).
The PassiveView pattern presented by Martin Fowler as an evolution of
MVP follows closely to this "dumbification" of the View.
On Sun, Sep 25, 2011 at 7:54 PM, Kevin Ollivier
<kevin...@theolliviers.com> wrote:
...
> Yeah, the problem is mostly over-thinking the level of abstraction. :) It
> will just give you a headache. Programmers are taught to think in
> abstractions, but there's a point where abstractions lose any practical
> benefit and actually cause you to come up with overly complex designs to
> solve simple problems. Even MVC vs. MVP seems to me to be based on a
> misunderstanding of what MVC is. MVP, from what I've read of it, reads like
> someone learning MVC by reading articles, misunderstanding it, and creating
> MVP to 'fix' what they saw was wrong with MVC, which IMHO is just
> re-creating MVC and calling it by another name. :) I will use the term MVC,
> but if you want to consider what I'm saying MVP that's fine.
--
Cheers
Override its OpenEditor method and only call the super classes method
for the cell(s) you want to let the editor open for.
Cody
Am 27.09.11 17:09, schrieb jmfauth:
On Sep 26, 2011, at 1:29 AM, Peter Damoc wrote:
> MVP might seam like a regurgitation of MVC by someone who fails to
> understand it but, IMHO it is not this.
>
> If I would have to describe the difference between MVC and MVP, I
> would point to the effort to make the View code as dumb as possible.
>
> The Controller code is broken in two: Interactor and Presenter where
> the Interactor provides insulation of the Presenter from the View.
>
> If done right, the Interactor and the View are dumb, dumb, dumb and
> are the only zones where something like wxPython appears.
> Being so dumb minimizes failure in these components. It also means
> that another toolkit could be used (theoretically).
Well, this is in my reading a variation on MVP (MVIP?), not MVP as originally constructed, but yeah, it's just adding one more layer of abstraction. Certainly useful for more complex scenarios.
How I see this sort of thing is that once you start thinking in terms of MVC, these sorts of evolutions happen naturally. Once you get into the habit of breaking things into discrete pieces, you may find that a particular app you're working on requires a 4th or 5th component, say, to allow web + native GUI instead of just native GUI alone. The key, I think, is to start people on that path so that they move away from designs that encourage speghetti code.
Regards,
Kevin
> The PassiveView pattern presented by Martin Fowler as an evolution of
> MVP follows closely to this "dumbification" of the View.
>
> On Sun, Sep 25, 2011 at 7:54 PM, Kevin Ollivier
> <kevin...@theolliviers.com> wrote:
> ...
>> Yeah, the problem is mostly over-thinking the level of abstraction. :) It
>> will just give you a headache. Programmers are taught to think in
>> abstractions, but there's a point where abstractions lose any practical
>> benefit and actually cause you to come up with overly complex designs to
>> solve simple problems. Even MVC vs. MVP seems to me to be based on a
>> misunderstanding of what MVC is. MVP, from what I've read of it, reads like
>> someone learning MVC by reading articles, misunderstanding it, and creating
>> MVP to 'fix' what they saw was wrong with MVC, which IMHO is just
>> re-creating MVC and calling it by another name. :) I will use the term MVC,
>> but if you want to consider what I'm saying MVP that's fine.
>
> --
> There is NO FATE, we are the creators.
> blog: http://damoc.ro/
>
I like that line -- I will remember it.
As someone who uses way more sarcasm than is necessary, I have to say
there is no evidence that I will ever run out. It seems to be the very
essence of a "renewable resource". Every time I try to slack off,
someone comes along and causes me to generate a lot more.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
I sure can take it and hope that all of you never run out of it ;-)
Cheers
Am 27.09.11 20:20, schrieb Tim Roberts:
> Tobias Weber wrote:
>> oops - will do.
>> Safe some of your sarcasm though - u sure do not wanna run out of it ;-)
> I like that line -- I will remember it.
>
> As someone who uses way more sarcasm than is necessary, I have to say
> there is no evidence that I will ever run out. It seems to be the very
> essence of a "renewable resource". Every time I try to slack off,
> someone comes along and causes me to generate a lot more.
>
--
--------------------------------------------------
Tobias Weber
CEO
The ROG Corporation GmbH
Donaustaufer Str. 200
93059 Regensburg
Tel: +49 941 4610 57 55
Fax: +49 941 4610 57 56
Gesch�ftsf�hrer: Tobias Weber
Registergericht: Amtsgericht Regensburg - HRB 8954
UStID DE225905250 - Steuer-Nr.184/59359
--------------------------------------------------
On Tue, Sep 27, 2011 at 6:52 PM, Kevin Ollivier
<kevin...@theolliviers.com> wrote:
...
> How I see this sort of thing is that once you start thinking in terms of MVC, these sorts of evolutions happen naturally. Once you get into the habit of breaking things into discrete pieces, you may find that a particular app you're working on requires a 4th or 5th component, say, to allow web + native GUI instead of just native GUI alone. The key, I think, is to start people on that path so that they move away from designs that encourage speghetti code.
What you said here cannot be overemphasized. Any kind of design is
better than no design at all.
My code naturally evolves towards spaghetti and if I don't intervene
I'm left with unmaintainable code.
It's a struggle.
Peter
I've found it difficult to create even a smallish app that can run
either/both wxPython GUI or command line. I need to spend more time
looking at MVC tutorials and discussion.
I have a development philosophy that can't be original, but have never
seen anyone give it a name:
When you set out to build an application:
First:
Write the libraries that you wish already existed to build you app
Then:
Put those libraries (and other pre-existing ones) together to build
your app.
By thinking in terms of libraries, it really helps me clarify where
given code should go -- is this specific to this particular app, or is
this a general-purpose method that anyone doing something similar would
want?
In the above case, the command line app and GUI app are two different
apps, but they would use (mostly) the same libraries--build those
libraries, and the apps will be pretty easy.
In fact, when you start out knowing you have two UIs to write, it should
be pretty easy -- if it'll only be used by the GUI, put it with the GUI
code, if it will only be used by the CLI, put it there, if it will be
used by both, put it in one of your libraries.
NOTE: libraries can be GUI libs too, if there is general purpose GUI
functionality in there.
NOTE 2: you can (should) write test code for your libraries that have
nothing to do with the UI as well -- so in that sense you always have to
interfaces to support -- your app UI, and your tests.
-HTH,
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Adding a new application that is aware of all the data, comms, etc, is
very fast. The suite is also pyexe converted to 15+ exes some dos some
windows...
Gadget/Steve
Agreed. I find myself doing this or something like it in most projects.
For my re-write I decided to start doing part of the sa model, and widget stuff and test them with the master data maintenance dialogs.
In an app like this where is the main code of e.g. "CreateItemClicked" method going?
Werner
# doesn't have to be here, this is just for example purposes
def OnInit(self):# if you wanted, you could actually initialize both the view and model inside controller.country's __init__ method, too.dialog = view.country(self, -1)data = model.country()self.countryController = controller.country(dialog, data)
Does this help?
I think so, will just have to give it a try and see.
On 09/30/2011 03:44 PM, werner wrote:On 09/29/2011 06:19 PM, Kevin Ollivier wrote:I think I got it.
...
I think so, will just have to give it a try and see.# doesn't have to be here, this is just for example purposesdef OnInit(self):# if you wanted, you could actually initialize both the view and model inside controller.country's __init__ method, too.dialog = view.country(self, -1)data = model.country()self.countryController = controller.country(dialog, data)
Does this help?
The thing which is feeling strange is the view as it is really just one line initializing some container widget.
My MasterDataController was previously a dialog which the country dialog (and all other master data dialogs) inherited, as it now is wx.EvtHandler based it allows me to re-factor that so it is not only usable for my master data dialogs/controllers but also for other things.
Kevin, thanks for opening my eyes.