]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-09/validator2.py
fix building/running of tex2rtf
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-09 / validator2.py
1 import wx
2 import pprint
3
4 about_txt = """\
5 The validator used in this example shows how the validator
6 can be used to transfer data to and from each text control
7 automatically when the dialog is shown and dismissed."""
8
9
10 class DataXferValidator(wx.PyValidator):
11 def __init__(self, data, key):
12 wx.PyValidator.__init__(self)
13 self.data = data
14 self.key = key
15
16 def Clone(self):
17 """
18 Note that every validator must implement the Clone() method.
19 """
20 return DataXferValidator(self.data, self.key)
21
22 def Validate(self, win):
23 return True
24
25 def TransferToWindow(self):
26 textCtrl = self.GetWindow()
27 textCtrl.SetValue(self.data.get(self.key, ""))
28 return True
29
30 def TransferFromWindow(self):
31 textCtrl = self.GetWindow()
32 self.data[self.key] = textCtrl.GetValue()
33 return True
34
35
36
37 class MyDialog(wx.Dialog):
38 def __init__(self, data):
39 wx.Dialog.__init__(self, None, -1, "Validators: data transfer")
40
41 # Create the text controls
42 about = wx.StaticText(self, -1, about_txt)
43 name_l = wx.StaticText(self, -1, "Name:")
44 email_l = wx.StaticText(self, -1, "Email:")
45 phone_l = wx.StaticText(self, -1, "Phone:")
46
47 name_t = wx.TextCtrl(self, validator=DataXferValidator(data, "name"))
48 email_t = wx.TextCtrl(self, validator=DataXferValidator(data, "email"))
49 phone_t = wx.TextCtrl(self, validator=DataXferValidator(data, "phone"))
50
51 # Use standard button IDs
52 okay = wx.Button(self, wx.ID_OK)
53 okay.SetDefault()
54 cancel = wx.Button(self, wx.ID_CANCEL)
55
56 # Layout with sizers
57 sizer = wx.BoxSizer(wx.VERTICAL)
58 sizer.Add(about, 0, wx.ALL, 5)
59 sizer.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
60
61 fgs = wx.FlexGridSizer(3, 2, 5, 5)
62 fgs.Add(name_l, 0, wx.ALIGN_RIGHT)
63 fgs.Add(name_t, 0, wx.EXPAND)
64 fgs.Add(email_l, 0, wx.ALIGN_RIGHT)
65 fgs.Add(email_t, 0, wx.EXPAND)
66 fgs.Add(phone_l, 0, wx.ALIGN_RIGHT)
67 fgs.Add(phone_t, 0, wx.EXPAND)
68 fgs.AddGrowableCol(1)
69 sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5)
70
71 btns = wx.StdDialogButtonSizer()
72 btns.AddButton(okay)
73 btns.AddButton(cancel)
74 btns.Realize()
75 sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 5)
76
77 self.SetSizer(sizer)
78 sizer.Fit(self)
79
80
81 app = wx.PySimpleApp()
82
83 data = { "name" : "Jordyn Dunn" }
84 dlg = MyDialog(data)
85 dlg.ShowModal()
86 dlg.Destroy()
87
88 wx.MessageBox("You entered these values:\n\n" +
89 pprint.pformat(data))
90
91 app.MainLoop()