]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/Validator.py
5 #----------------------------------------------------------------------
10 class MyValidator(wx
.PyValidator
):
11 def __init__(self
, flag
=None, pyVar
=None):
12 wx
.PyValidator
.__init
__(self
)
14 self
.Bind(wx
.EVT_CHAR
, self
.OnChar
)
17 return MyValidator(self
.flag
)
19 def Validate(self
, win
):
23 if self
.flag
== ALPHA_ONLY
:
25 if x
not in string
.letters
:
28 elif self
.flag
== DIGIT_ONLY
:
30 if x
not in string
.digits
:
36 def OnChar(self
, event
):
39 if key
< wx
.WXK_SPACE
or key
== wx
.WXK_DELETE
or key
> 255:
43 if self
.flag
== ALPHA_ONLY
and chr(key
) in string
.letters
:
47 if self
.flag
== DIGIT_ONLY
and chr(key
) in string
.digits
:
51 if not wx
.Validator_IsSilent():
54 # Returning without calling even.Skip eats the event before it
55 # gets to the text control
58 #----------------------------------------------------------------------
60 class TestValidatorPanel(wx
.Panel
):
61 def __init__(self
, parent
):
62 wx
.Panel
.__init
__(self
, parent
, -1)
63 self
.SetAutoLayout(True)
66 fgs
= wx
.FlexGridSizer(0, 2)
69 fgs
.Add(wx
.StaticText(self
, -1, "These controls have validators that limit\n"
70 "the type of characters that can be entered."))
72 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
74 label
= wx
.StaticText(self
, -1, "Alpha Only: ")
75 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
77 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= MyValidator(ALPHA_ONLY
)))
79 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
81 label
= wx
.StaticText(self
, -1, "Digits Only: ")
82 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
83 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= MyValidator(DIGIT_ONLY
)))
85 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
86 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
88 b
= wx
.Button(self
, -1, "Test Dialog Validation")
89 self
.Bind(wx
.EVT_BUTTON
, self
.OnDoDialog
, b
)
92 border
= wx
.BoxSizer()
93 border
.Add(fgs
, 1, wx
.GROW|wx
.ALL
, 25)
97 def OnDoDialog(self
, evt
):
98 dlg
= TestValidateDialog(self
)
103 #----------------------------------------------------------------------
105 class TextObjectValidator(wx
.PyValidator
):
106 """ This validator is used to ensure that the user has entered something
107 into the text object editor dialog's text field.
110 """ Standard constructor.
112 wx
.PyValidator
.__init
__(self
)
119 Note that every validator must implement the Clone() method.
121 return TextObjectValidator()
124 def Validate(self
, win
):
125 """ Validate the contents of the given text control.
127 textCtrl
= self
.GetWindow()
128 text
= textCtrl
.GetValue()
131 wx
.MessageBox("A text object must contain some text!", "Error")
132 textCtrl
.SetBackgroundColour("pink")
137 textCtrl
.SetBackgroundColour(
138 wx
.SystemSettings_GetColour(wx
.SYS_COLOUR_WINDOW
))
143 def TransferToWindow(self
):
144 """ Transfer data from validator to window.
146 The default implementation returns False, indicating that an error
147 occurred. We simply return True, as we don't do any data transfer.
149 return True # Prevent wxDialog from complaining.
152 def TransferFromWindow(self
):
153 """ Transfer data from window to validator.
155 The default implementation returns False, indicating that an error
156 occurred. We simply return True, as we don't do any data transfer.
158 return True # Prevent wxDialog from complaining.
160 #----------------------------------------------------------------------
162 class TestValidateDialog(wx
.Dialog
):
163 def __init__(self
, parent
):
164 wx
.Dialog
.__init
__(self
, parent
, -1, "Validated Dialog")
166 self
.SetAutoLayout(True)
169 fgs
= wx
.FlexGridSizer(0, 2)
172 fgs
.Add(wx
.StaticText(self
, -1,
173 "These controls must have text entered into them. Each\n"
174 "one has a validator that is checked when the Okay\n"
175 "button is clicked."))
177 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
179 label
= wx
.StaticText(self
, -1, "First: ")
180 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
182 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= TextObjectValidator()))
184 fgs
.Add((1,VSPACE
)); fgs
.Add((1,VSPACE
))
186 label
= wx
.StaticText(self
, -1, "Second: ")
187 fgs
.Add(label
, 0, wx
.ALIGN_RIGHT|wx
.CENTER
)
188 fgs
.Add(wx
.TextCtrl(self
, -1, "", validator
= TextObjectValidator()))
191 buttons
= wx
.BoxSizer(wx
.HORIZONTAL
)
192 b
= wx
.Button(self
, wx
.ID_OK
, "Okay")
194 buttons
.Add(b
, 0, wx
.ALL
, 10)
195 buttons
.Add(wx
.Button(self
, wx
.ID_CANCEL
, "Cancel"), 0, wx
.ALL
, 10)
197 border
= wx
.BoxSizer(wx
.VERTICAL
)
198 border
.Add(fgs
, 1, wx
.GROW|wx
.ALL
, 25)
200 self
.SetSizer(border
)
205 #----------------------------------------------------------------------
207 def runTest(frame
, nb
, log
):
208 win
= TestValidatorPanel(nb
)
211 #----------------------------------------------------------------------
218 wx.Validator is the base class for a family of validator classes that mediate
219 between a class of control, and application data.
221 <p>A validator has three major roles:
224 <li>to transfer data from a C++ variable or own storage to and from a control;
225 <li>to validate data in a control, and show an appropriate error message;
226 <li>to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
228 <p>Validators can be plugged into controls dynamically.
235 if __name__
== '__main__':
238 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])