Custom model file abtractmodel.py:
1 classAbstractmodel (object):2 3 def __init__(self):4Self.listeners = []5 6 defAddListener (Self, listenerfunc):7 self.listeners.append (Listenerfunc)8 9 defRemoveListener (Self, listenerfunc):Ten Self.listeners.remove (Listenerfunc) One A defUpdate (self): - forEachfuncinchself.listeners: -Eachfunc (self)
The instance calls the MVC file modelexample.py:
1 #!usr/bin/env python2 3 ImportWX4 ImportAbstractmodel5 6 7 classSimplename (Abstractmodel. Abstractmodel):8 def __init__(self,first="", last=""):9Abstractmodel. Abstractmodel.__init__(self)Ten Self.set (first,last) One A defSet (self,first,last): -Self.first = First -Self.last = Last the self.update () - - classmodelexample (WX. Frame): - def __init__(self,parent,id): +Wx. Frame.__init__(Self,parent,id,'Flintstones', Size= (340,200)) -Panel =WX. Panel (self) +Panel. Setbackgroundcolour (" White") A Self . Bind (WX. Evt_close,self. Onclosewindow) atSelf.textfields = {} - self.createtextfields (panel) -Self.model =Simplename () - Self.model.addListener (self. OnUpdate) - Self.createbuttonbar (panel) - in defButtondata (self): - return(("fredify", self. onfred), to("Wilmafy", self. Onwilma), +("barnify", self. Onbarney), -("bettify", self. Onbetty)) the * defCreatebuttonbar (self,panel,ypos=0): $xpos=0Panax Notoginseng forEachlabel,eachhandlerinchself.buttondata (): -pos =(Xpos,ypos) thebutton =Self.buildonebutton (Panel,eachlabel,eachhandler,pos) +XPos + =button. GetSize (). Width A the defBuildonebutton (self,parent,label,handler,pos=(0,0)): +button = WX. Button (parent,-1, Label,pos) - Self . Bind (WX. Evt_button,handler,button) $ returnButton $ - defTextfielddata (self): - return(("First Name", (10,50)), ("Last Name", (10,80))) the - defCreatetextfields (self,panel):Wuyi forEachlabel,eachposinchself.textfielddata (): the Self.createcaptionedtext (Panel,eachlabel,eachpos) - Wu defCreatecaptionedtext (self,panel,label,pos): -static =WX. Statictext (panel,wx. NewId (), Label,pos) AboutStatic. Setbackgroundcolour (" White") $Textpos = (pos[0]+75,pos[1]) -Self.textfields[label] =WX. Textctrl (panel,wx. NewId (), - "", Size= (100,-1), pos=Textpos, -style=wx.te_readonly) A + defOnUpdate (Self,model): theself.textfields["First Name"]. SetValue (Model.first) -self.textfields["Last Name"]. SetValue (model.last) $ the defonfred (self,event): theSelf.model.set ("Fred","Flintstones") the the defOnbarney (self,event): -Self.model.set ("Barney","Rubble") in the defOnwilma (self,event): theSelf.model.set ("Wilma","Flintstones") About the defOnbetty (self,event): theSelf.model.set ("Betty","Rubble") the + defOnclosewindow (self,event): - Self . Destroy () the Bayi if __name__=='__main__': theApp =WX. Pysimpleapp () theframe = Modelexample (parent=none,id=-1) - frame. Show () - app. Mainloop () the
Wxpython Getting Started practice code four