]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-05/modelExample.py
don't use strlen() to verify the length of the string as it can contain embedded...
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-05 / modelExample.py
1 #!/usr/bin/env python
2
3 import wx
4 import abstractmodel
5
6 class SimpleName(abstractmodel.AbstractModel):
7
8 def __init__(self, first="", last=""):
9 abstractmodel.AbstractModel.__init__(self)
10 self.set(first, last)
11
12 def set(self, first, last):
13 self.first = first
14 self.last = last
15 self.update()
16
17 class ModelExample(wx.Frame):
18
19 def __init__(self, parent, id):
20 wx.Frame.__init__(self, parent, id, 'Flintstones',
21 size=(340, 200))
22 panel = wx.Panel(self)
23 panel.SetBackgroundColour("White")
24 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
25 self.textFields = {}
26 self.createTextFields(panel)
27 self.model = SimpleName()
28 self.model.addListener(self.OnUpdate)
29 self.createButtonBar(panel)
30
31 def buttonData(self):
32 return (("Fredify", self.OnFred),
33 ("Wilmafy", self.OnWilma),
34 ("Barnify", self.OnBarney),
35 ("Bettify", self.OnBetty))
36
37 def createButtonBar(self, panel, yPos = 0):
38 xPos = 0
39 for eachLabel, eachHandler in self.buttonData():
40 pos = (xPos, yPos)
41 button = self.buildOneButton(panel, eachLabel, eachHandler, pos)
42 xPos += button.GetSize().width
43
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)
47 return button
48
49 def textFieldData(self):
50 return (("First Name", (10, 50)),
51 ("Last Name", (10, 80)))
52
53 def createTextFields(self, panel):
54 for eachLabel, eachPos in self.textFieldData():
55 self.createCaptionedText(panel, eachLabel, eachPos)
56
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,
63 style=wx.TE_READONLY)
64
65 def OnUpdate(self, model):
66 self.textFields["First Name"].SetValue(model.first)
67 self.textFields["Last Name"].SetValue(model.last)
68
69 def OnFred(self, event):
70 self.model.set("Fred", "Flintstone")
71
72 def OnBarney(self, event):
73 self.model.set("Barney", "Rubble")
74
75 def OnWilma(self, event):
76 self.model.set("Wilma", "Flintstone")
77
78 def OnBetty(self, event):
79 self.model.set("Betty", "Rubble")
80
81 def OnCloseWindow(self, event):
82 self.Destroy()
83
84 if __name__ == '__main__':
85 app = wx.PySimpleApp()
86 frame = ModelExample(parent=None, id=-1)
87 frame.Show()
88 app.MainLoop()
89