]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-09/validator3.py
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.
12 class CharValidator(wx
.PyValidator
):
13 def __init__(self
, flag
):
14 wx
.PyValidator
.__init
__(self
)
16 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
20 Note that every validator must implement the Clone() method.
22 return CharValidator(self
.flag
)
24 def Validate(self
, win
):
27 def TransferToWindow(self
):
30 def TransferFromWindow(self
):
33 def OnChar(self
, evt
):
34 key
= chr(evt
.GetKeyCode())
35 if self
.flag
== "no-alpha" and key
in string
.letters
:
37 if self
.flag
== "no-digit" and key
in string
.digits
:
42 class MyDialog(wx
.Dialog
):
44 wx
.Dialog
.__init
__(self
, None, -1, "Validators: behavior modification")
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:")
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"))
56 # Use standard button IDs
57 okay
= wx
.Button(self
, wx
.ID_OK
)
59 cancel
= wx
.Button(self
, wx
.ID_CANCEL
)
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)
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
)
74 sizer
.Add(fgs
, 0, wx
.EXPAND|wx
.ALL
, 5)
76 btns
= wx
.StdDialogButtonSizer()
78 btns
.AddButton(cancel
)
80 sizer
.Add(btns
, 0, wx
.EXPAND|wx
.ALL
, 5)
86 app
= wx
.PySimpleApp()