Model View Presenter GUI pattern?

george young gry at ll.mit.edu
Thu Dec 13 17:28:33 EST 2001


Is anyone using the Model View Presenter GUI framework/pattern in python?
I've been reading bits about it, and it sounds quite attractive.  

I've started trying to write a simple MVP module as a basis for an application.
The hardest part is dealing with the overall structure:  my app deals with a
big structured data object and it's hard to picture how the parallel trees of 
models, views, and presenters all get instantiated and linked.   

Below I include my baby mvp.py file (using pygtk) for comment/discussion.  Any comments of 
design/implementation/style etc. are very welcome.

   A few mvp references for the curious:
http://www.object-arts.com/Lib/EducationCentre4/htm/modelviewpresenter.htm
http://www.object-arts.com/EducationCentre/Overviews/ModelViewPresenter.htm
http://www-106.ibm.com/developerworks/library/mvp.html


import gtk
class Topw(gtk.GtkWindow):
    def __init__(self):
        gtk.GtkWindow.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.connect("destroy", gtk.mainquit)
        self.connect("delete_event", gtk.mainquit)


class Observable:
    def __init__(self):
        self.observers = []
    def register(self, o):
        self.observers.append(o)
    def un_register(self, o):
        self.observers.remove(o)
    def notify(self):
        for o in self.observers:
            o.update(self)

class Observer:
    '''Just for clarity, not really needed.'''
    def update(self): pass


class Model(Observable):
    def __init__(self, value=None):
        Observable.__init__(self)
        self.set(value)
    def get(self):
        return self._value
    def set(self, v):
        self._value = v
    def str(self):
        return `self.get()`


class IntegerModel(Model):
    def __init__(self, i):
        Model.__init__(self, i)
    def set(self, i):
        self._value = int(i)
            

class IntegerView(gtk.GtkEntry, Observer):
    def __init__(self):
        gtk.GtkEntry.__init__(self)
        
    def set(self, x):
        self.set_text(`x`)
        
    def clear(self):
        self.delete_text(0, -1)

    def update(self, mod):
        self.set(mod.get())
        

class IntegerPresenter:
    def __init__(self, i=None):
        self.view = IntegerView()
        self.model = IntegerModel(i)
        self.model.register(self.view)
        self.view.connect('changed', self.changed)

    def changed(self, v):
        try:
            self.model.set(int(v.get_text()))
        except ValueError:
            gtk.gdk_beep()


if __name__ == '__main__':
    pres = IntegerPresenter(33)
    topw = Topw()
    topw.add(pres.view)
    topw.show_all()
    gtk.mainloop()



-- 
 I cannot think why the whole bed of the ocean is
 not one solid mass of oysters, so prolific they seem. Ah,
 I am wandering! Strange how the brain controls the brain!
	-- Sherlock Holmes in "The Dying Detective"



More information about the Python-list mailing list