]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-05/modelExample.py
6 class SimpleName(abstractmodel
.AbstractModel
):
8 def __init__(self
, first
="", last
=""):
9 abstractmodel
.AbstractModel
.__init
__(self
)
12 def set(self
, first
, last
):
17 class ModelExample(wx
.Frame
):
19 def __init__(self
, parent
, id):
20 wx
.Frame
.__init
__(self
, parent
, id, 'Flintstones',
22 panel
= wx
.Panel(self
)
23 panel
.SetBackgroundColour("White")
24 self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
)
26 self
.createTextFields(panel
)
27 self
.model
= SimpleName()
28 self
.model
.addListener(self
.OnUpdate
)
29 self
.createButtonBar(panel
)
32 return (("Fredify", self
.OnFred
),
33 ("Wilmafy", self
.OnWilma
),
34 ("Barnify", self
.OnBarney
),
35 ("Bettify", self
.OnBetty
))
37 def createButtonBar(self
, panel
, yPos
= 0):
39 for eachLabel
, eachHandler
in self
.buttonData():
41 button
= self
.buildOneButton(panel
, eachLabel
, eachHandler
, pos
)
42 xPos
+= button
.GetSize().width
44 def buildOneButton(self
, parent
, label
, handler
, pos
=(0,0)):
45 button
= wx
.Button(parent
, -1, label
, pos
)
46 self
.Bind(wx
.EVT_BUTTON
, handler
, button
)
49 def textFieldData(self
):
50 return (("First Name", (10, 50)),
51 ("Last Name", (10, 80)))
53 def createTextFields(self
, panel
):
54 for eachLabel
, eachPos
in self
.textFieldData():
55 self
.createCaptionedText(panel
, eachLabel
, eachPos
)
57 def createCaptionedText(self
, panel
, label
, pos
):
58 static
= wx
.StaticText(panel
, wx
.NewId(), label
, pos
)
59 static
.SetBackgroundColour("White")
60 textPos
= (pos
[0] + 75, pos
[1])
61 self
.textFields
[label
] = wx
.TextCtrl(panel
, wx
.NewId(),
62 "", size
=(100, -1), pos
=textPos
,
65 def OnUpdate(self
, model
):
66 self
.textFields
["First Name"].SetValue(model
.first
)
67 self
.textFields
["Last Name"].SetValue(model
.last
)
69 def OnFred(self
, event
):
70 self
.model
.set("Fred", "Flintstone")
72 def OnBarney(self
, event
):
73 self
.model
.set("Barney", "Rubble")
75 def OnWilma(self
, event
):
76 self
.model
.set("Wilma", "Flintstone")
78 def OnBetty(self
, event
):
79 self
.model
.set("Betty", "Rubble")
81 def OnCloseWindow(self
, event
):
84 if __name__
== '__main__':
85 app
= wx
.PySimpleApp()
86 frame
= ModelExample(parent
=None, id=-1)