]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Validator.py
1 # 11/22/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
10 #----------------------------------------------------------------------
15 class MyValidator(wx
.PyValidator
):
16 def __init__(self
, flag
=None, pyVar
=None):
17 wx
.PyValidator
.__init
__(self
)
19 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
22 return MyValidator(self
.flag
)
24 def Validate(self
, win
):
28 if self
.flag
== ALPHA_ONLY
:
30 if x
not in string
.letters
:
33 elif self
.flag
== DIGIT_ONLY
:
35 if x
not in string
.digits
:
41 def OnChar(self
, event
):
44 if key
< wx
.WXK_SPACE
or key
== wx
.WXK_DELETE
or key
> 255:
48 if self
.flag
== ALPHA_ONLY
and chr(key
) in string
.letters
:
52 if self
.flag
== DIGIT_ONLY
and chr(key
) in string
.digits
:
56 if not wx
.Validator_IsSilent():
59 # Returning without calling even.Skip eats the event before it
60 # gets to the text control
63 #----------------------------------------------------------------------
65 class TestValidatorPanel(wx
.Panel
):
66 def __init__(self
, parent
):
67 wx
.Panel
.__init
__(self
, parent
, -1)
68 self
.SetAutoLayout(True)
71 fgs
= wx
.FlexGridSizer(0, 2)
74 fgs
.Add(wx
.StaticText(self
, -1, "These controls have validators that limit\n"
75 "the type of characters that can be entered."))
77 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
79 label
= wx
.StaticText(self
, -1, "Alpha Only: ")
80 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
82 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= MyValidator(ALPHA_ONLY
)))
84 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
86 label
= wx
.StaticText(self
, -1, "Digits Only: ")
87 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
88 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= MyValidator(DIGIT_ONLY
)))
90 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
91 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
93 b
= wx
.Button(self
, -1, "Test Dialog Validation")
94 self
.Bind(wx
.EVT_BUTTON
, self
.OnDoDialog
, b
)
97 border
= wx
.BoxSizer()
98 border
.Add(fgs
, 1, wx
.GROW|wx
.ALL
, 25)
102 def OnDoDialog(self
, evt
):
103 dlg
= TestValidateDialog(self
)
108 #----------------------------------------------------------------------
110 class TextObjectValidator(wx
.PyValidator
):
111 """ This validator is used to ensure that the user has entered something
112 into the text object editor dialog's text field.
115 """ Standard constructor.
117 wx
.PyValidator
.__init
__(self
)
124 Note that every validator must implement the Clone() method.
126 return TextObjectValidator()
129 def Validate(self
, win
):
130 """ Validate the contents of the given text control.
132 textCtrl
= self
.GetWindow()
133 text
= textCtrl
.GetValue()
136 wx
.MessageBox("A text object must contain some text!", "Error")
137 textCtrl
.SetBackgroundColour("pink")
142 textCtrl
.SetBackgroundColour(
143 wx
.SystemSettings_GetColour(wx
.SYS_COLOUR_WINDOW
))
148 def TransferToWindow(self
):
149 """ Transfer data from validator to window.
151 The default implementation returns False, indicating that an error
152 occurred. We simply return True, as we don't do any data transfer.
154 return True # Prevent wxDialog from complaining.
157 def TransferFromWindow(self
):
158 """ Transfer data from window to validator.
160 The default implementation returns False, indicating that an error
161 occurred. We simply return True, as we don't do any data transfer.
163 return True # Prevent wxDialog from complaining.
165 #----------------------------------------------------------------------
167 class TestValidateDialog(wx
.Dialog
):
168 def __init__(self
, parent
):
169 wx
.Dialog
.__init
__(self
, parent
, -1, "Validated Dialog")
171 self
.SetAutoLayout(True)
174 fgs
= wx
.FlexGridSizer(0, 2)
177 fgs
.Add(wx
.StaticText(self
, -1,
178 "These controls must have text entered into them. Each\n"
179 "one has a validator that is checked when the Okay\n"
180 "button is clicked."))
182 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
184 label
= wx
.StaticText(self
, -1, "First: ")
185 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
187 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= TextObjectValidator()))
189 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
191 label
= wx
.StaticText(self
, -1, "Second: ")
192 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
193 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= TextObjectValidator()))
196 buttons
= wx
.BoxSizer(wx
.HORIZONTAL
)
197 b
= wx
.Button(self
, wx
.ID_OK
, "Okay")
199 buttons
.Add(b
, 0, wx
.ALL
, 10)
200 buttons
.Add(wx
.Button(self
, wx
.ID_CANCEL
, "Cancel"), 0, wx
.ALL
, 10)
202 border
= wx
.BoxSizer(wx
.VERTICAL
)
203 border
.Add(fgs
, 1, wx
.GROW|wx
.ALL
, 25)
205 self
.SetSizer(border
)
210 #----------------------------------------------------------------------
212 def runTest(frame
, nb
, log
):
213 win
= TestValidatorPanel(nb
)
216 #----------------------------------------------------------------------
223 wxValidator is the base class for a family of validator classes that mediate
224 between a class of control, and application data.
226 <p>A validator has three major roles:
229 <li>to transfer data from a C++ variable or own storage to and from a control;
230 <li>to validate data in a control, and show an appropriate error message;
231 <li>to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
233 <p>Validators can be plugged into controls dynamically.
240 if __name__
== '__main__':
243 run
.main(['', os
.path
.basename(sys
.argv
[0])])