#! /usr/bin/env python
#coding=utf-8
import wx
class AbstractModel(object):
def __init__(self):
self.listeners = []
def addListener(self, listenerFunc):
self.listeners.append(listenerFunc)
def removeListener(self, listenerFunc):
self.listeners.remove(listenerFunc)
#此方法很关键,可以在model改变时候更新View
def update(self):
for eachFunc in self.listeners:
eachFunc(self)
class SimpleName(AbstractModel):
'''定义的Model类'''
def __init__(self, first="", last=""):
AbstractModel.__init__(self)
self.set(first, last)
def set(self, first, last):
self.first = first
self.last = last
self.update() #1 更新
class ModelExample(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Flintstones',
size=(340, 200))
panel = wx.Panel(self)
panel.SetBackgroundColour("White")
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.textFields = {}
self.createTextFields(panel)
#-------------------------------
#2 创建模型
self.model = SimpleName()
#绑定需要通知视图的方法
self.model.addListener(self.OnUpdate)
#-------------------------------
self.createButtonBar(panel)
def buttonData(self):
return (("Fredify", self.OnFred),
("Wilmafy", self.OnWilma),
("Barnify", self.OnBarney),
("Bettify", self.OnBetty))
def createButtonBar(self, panel, yPos = 0):
xPos = 0
for eachLabel, eachHandler in self.buttonData():
pos = (xPos, yPos)
button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
xPos += button.GetSize().width
def buildOneButton(self, parent, label, handler, pos=(0,0)):
button = wx.Button(parent, -1, label, pos)
self.Bind(wx.EVT_BUTTON, handler, button)
return button
def textFieldData(self):
return (("First Name", (10, 50)),
("Last Name", (10, 80)))
def createTextFields(self, panel):
for eachLabel, eachPos in self.textFieldData():
self.createCaptionedText(panel, eachLabel, eachPos)
def createCaptionedText(self, panel, label, pos):
static = wx.StaticText(panel, wx.NewId(), label, pos)
static.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)
def OnUpdate(self, model): #3 设置文本域
self.textFields["First Name"].SetValue(model.first)
self.textFields["Last Name"].SetValue(model.last)
#-------------------------------------------
#4 响应按钮敲击的处理器
def OnFred(self, event):
self.model.set("Fred", "Flintstone")
def OnBarney(self, event):
self.model.set("Barney", "Rubble")
def OnWilma(self, event):
self.model.set("Wilma", "Flintstone")
def OnBetty(self, event):
self.model.set("Betty", "Rubble")
#---------------------------------------------
def OnCloseWindow(self, event):
self.Destroy()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = ModelExample(parent=None, id=-1)
frame.Show()
app.MainLoop()