]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxValidator.py
2 from wxPython
.wx
import *
3 from wxPython
.lib
.grids
import wxFlexGridSizer
7 #----------------------------------------------------------------------
12 class MyValidator(wxPyValidator
):
13 def __init__(self
, flag
=None, pyVar
=None):
14 wxPyValidator
.__init
__(self
)
16 EVT_CHAR(self
, self
.OnChar
)
19 return MyValidator(self
.flag
)
21 def Validate(self
, win
):
23 tc
= wxPyTypeCast(win
, "wxTextCtrl")
25 if self
.flag
== ALPHA_ONLY
:
27 if x
not in string
.letters
:
30 elif self
.flag
== DIGIT_ONLY
:
32 if x
not in string
.digits
:
38 def OnChar(self
, event
):
40 if key
< WXK_SPACE
or key
== WXK_DELETE
or key
> 255:
43 if self
.flag
== ALPHA_ONLY
and chr(key
) in string
.letters
:
46 if self
.flag
== DIGIT_ONLY
and chr(key
) in string
.digits
:
50 if not wxValidator_IsSilent():
53 # Returning without calling even.Skip eats the event before it
54 # gets to the text control
57 #----------------------------------------------------------------------
59 class TestValidatorPanel(wxPanel
):
60 def __init__(self
, parent
):
61 wxPanel
.__init
__(self
, parent
, -1)
62 self
.SetAutoLayout(true
)
65 fgs
= wxFlexGridSizer(0, 2)
68 fgs
.Add(wxStaticText(self
, -1, "These controls have validators that limit\n"
69 "the type of characters that can be entered."))
71 fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
)
73 label
= wxStaticText(self
, -1, "Alpha Only: ")
74 fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
)
76 fgs
.Add(wxTextCtrl(self
, -1, "", validator
= MyValidator(ALPHA_ONLY
)))
78 fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
)
80 label
= wxStaticText(self
, -1, "Digits Only: ")
81 fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
)
82 fgs
.Add(wxTextCtrl(self
, -1, "", validator
= MyValidator(DIGIT_ONLY
)))
85 border
.Add(fgs
, 1, wxGROW|wxALL
, 25)
90 #----------------------------------------------------------------------
92 def runTest(frame
, nb
, log
):
93 win
= TestValidatorPanel(nb
)
96 #----------------------------------------------------------------------
107 wxValidator is the base class for a family of validator classes that mediate between a class of control, and application data.
109 A validator has three major roles:
111 1. to transfer data from a C++ variable or own storage to and from a control;
113 2. to validate data in a control, and show an appropriate error message;
115 3. to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
117 Validators can be plugged into controls dynamically.