]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-09/validator3.py
don't use strlen() to verify the length of the string as it can contain embedded...
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-09 / validator3.py
1 import wx
2 import string
3
4 about_txt = """\
5 The validator used in this example will validate the input on the fly
6 instead of waiting until the okay button is pressed. The first field
7 will not allow digits to be typed, the second will allow anything
8 and the third will not allow alphabetic characters to be entered.
9 """
10
11
12 class CharValidator(wx.PyValidator):
13 def __init__(self, flag):
14 wx.PyValidator.__init__(self)
15 self.flag = flag
16 self.Bind(wx.EVT_CHAR, self.OnChar)
17
18 def Clone(self):
19 """
20 Note that every validator must implement the Clone() method.
21 """
22 return CharValidator(self.flag)
23
24 def Validate(self, win):
25 return True
26
27 def TransferToWindow(self):
28 return True
29
30 def TransferFromWindow(self):
31 return True
32
33 def OnChar(self, evt):
34 key = chr(evt.GetKeyCode())
35 if self.flag == "no-alpha" and key in string.letters:
36 return
37 if self.flag == "no-digit" and key in string.digits:
38 return
39 evt.Skip()
40
41
42 class MyDialog(wx.Dialog):
43 def __init__(self):
44 wx.Dialog.__init__(self, None, -1, "Validators: behavior modification")
45
46 # Create the text controls
47 about = wx.StaticText(self, -1, about_txt)
48 name_l = wx.StaticText(self, -1, "Name:")
49 email_l = wx.StaticText(self, -1, "Email:")
50 phone_l = wx.StaticText(self, -1, "Phone:")
51
52 name_t = wx.TextCtrl(self, validator=CharValidator("no-digit"))
53 email_t = wx.TextCtrl(self, validator=CharValidator("any"))
54 phone_t = wx.TextCtrl(self, validator=CharValidator("no-alpha"))
55
56 # Use standard button IDs
57 okay = wx.Button(self, wx.ID_OK)
58 okay.SetDefault()
59 cancel = wx.Button(self, wx.ID_CANCEL)
60
61 # Layout with sizers
62 sizer = wx.BoxSizer(wx.VERTICAL)
63 sizer.Add(about, 0, wx.ALL, 5)
64 sizer.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
65
66 fgs = wx.FlexGridSizer(3, 2, 5, 5)
67 fgs.Add(name_l, 0, wx.ALIGN_RIGHT)
68 fgs.Add(name_t, 0, wx.EXPAND)
69 fgs.Add(email_l, 0, wx.ALIGN_RIGHT)
70 fgs.Add(email_t, 0, wx.EXPAND)
71 fgs.Add(phone_l, 0, wx.ALIGN_RIGHT)
72 fgs.Add(phone_t, 0, wx.EXPAND)
73 fgs.AddGrowableCol(1)
74 sizer.Add(fgs, 0, wx.EXPAND|wx.ALL, 5)
75
76 btns = wx.StdDialogButtonSizer()
77 btns.AddButton(okay)
78 btns.AddButton(cancel)
79 btns.Realize()
80 sizer.Add(btns, 0, wx.EXPAND|wx.ALL, 5)
81
82 self.SetSizer(sizer)
83 sizer.Fit(self)
84
85
86 app = wx.PySimpleApp()
87
88 dlg = MyDialog()
89 dlg.ShowModal()
90 dlg.Destroy()
91
92 app.MainLoop()