]>
git.saurik.com Git - wxWidgets.git/blob - 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
): 
  24         if self
.flag 
== ALPHA_ONLY
: 
  26                 if x 
not in string
.letters
: 
  29         elif self
.flag 
== DIGIT_ONLY
: 
  31                 if x 
not in string
.digits
: 
  37     def OnChar(self
, event
): 
  39         if key 
< WXK_SPACE 
or key 
== WXK_DELETE 
or key 
> 255: 
  42         if self
.flag 
== ALPHA_ONLY 
and chr(key
) in string
.letters
: 
  45         if self
.flag 
== DIGIT_ONLY 
and chr(key
) in string
.digits
: 
  49         if not wxValidator_IsSilent(): 
  52         # Returning without calling even.Skip eats the event before it 
  53         # gets to the text control 
  56 #---------------------------------------------------------------------- 
  58 class TestValidatorPanel(wxPanel
): 
  59     def __init__(self
, parent
): 
  60         wxPanel
.__init
__(self
, parent
, -1) 
  61         self
.SetAutoLayout(true
) 
  64         fgs 
= wxFlexGridSizer(0, 2) 
  67         fgs
.Add(wxStaticText(self
, -1, "These controls have validators that limit\n" 
  68                              "the type of characters that can be entered.")) 
  70         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
  72         label 
= wxStaticText(self
, -1, "Alpha Only: ") 
  73         fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
) 
  75         fgs
.Add(wxTextCtrl(self
, -1, "", validator 
= MyValidator(ALPHA_ONLY
))) 
  77         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
  79         label 
= wxStaticText(self
, -1, "Digits Only: ") 
  80         fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
) 
  81         fgs
.Add(wxTextCtrl(self
, -1, "", validator 
= MyValidator(DIGIT_ONLY
))) 
  83         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
  84         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
  86         b 
= wxButton(self
, -1, "Test Dialog Validation") 
  87         EVT_BUTTON(self
, b
.GetId(), self
.OnDoDialog
) 
  91         border
.Add(fgs
, 1, wxGROW|wxALL
, 25) 
  95     def OnDoDialog(self
, evt
): 
  96         dlg 
= TestValidateDialog(self
) 
 101 #---------------------------------------------------------------------- 
 103 class TextObjectValidator(wxPyValidator
): 
 104      """ This validator is used to ensure that the user has entered something 
 105          into the text object editor dialog's text field. 
 108          """ Standard constructor. 
 110          wxPyValidator
.__init
__(self
) 
 117              Note that every validator must implement the Clone() method. 
 119          return TextObjectValidator() 
 122      def Validate(self
, win
): 
 123          """ Validate the contents of the given text control. 
 125          textCtrl 
= self
.GetWindow() 
 126          text 
= textCtrl
.GetValue() 
 129              wxMessageBox("A text object must contain some text!", "Error") 
 130              textCtrl
.SetBackgroundColour("pink") 
 135              textCtrl
.SetBackgroundColour( 
 136                  wxSystemSettings_GetColour(wxSYS_COLOUR_WINDOW
)) 
 141      def TransferToWindow(self
): 
 142          """ Transfer data from validator to window. 
 144              The default implementation returns false, indicating that an error 
 145              occurred.  We simply return true, as we don't do any data transfer. 
 147          return true 
# Prevent wxDialog from complaining. 
 150      def TransferFromWindow(self
): 
 151          """ Transfer data from window to validator. 
 153              The default implementation returns false, indicating that an error 
 154              occurred.  We simply return true, as we don't do any data transfer. 
 156          return true 
# Prevent wxDialog from complaining. 
 158 #---------------------------------------------------------------------- 
 160 class TestValidateDialog(wxDialog
): 
 161     def __init__(self
, parent
): 
 162         wxDialog
.__init
__(self
, parent
, -1, "Validated Dialog") 
 164         self
.SetAutoLayout(true
) 
 167         fgs 
= wxFlexGridSizer(0, 2) 
 170         fgs
.Add(wxStaticText(self
, -1, 
 171                              "These controls must have text entered into them.  Each\n" 
 172                              "one has a validator that is checked when the Okay\n" 
 173                              "button is clicked.")) 
 175         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
 177         label 
= wxStaticText(self
, -1, "First: ") 
 178         fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
) 
 180         fgs
.Add(wxTextCtrl(self
, -1, "", validator 
= TextObjectValidator())) 
 182         fgs
.Add(1,VSPACE
); fgs
.Add(1,VSPACE
) 
 184         label 
= wxStaticText(self
, -1, "Second: ") 
 185         fgs
.Add(label
, 0, wxALIGN_RIGHT|wxCENTER
) 
 186         fgs
.Add(wxTextCtrl(self
, -1, "", validator 
= TextObjectValidator())) 
 189         buttons 
= wxBoxSizer(wxHORIZONTAL
) 
 190         b 
= wxButton(self
, wxID_OK
, "Okay") 
 192         buttons
.Add(b
, 0, wxALL
, 10) 
 193         buttons
.Add(wxButton(self
, wxID_CANCEL
, "Cancel"), 0, wxALL
, 10) 
 195         border 
= wxBoxSizer(wxVERTICAL
) 
 196         border
.Add(fgs
, 1, wxGROW|wxALL
, 25) 
 198         self
.SetSizer(border
) 
 203 #---------------------------------------------------------------------- 
 205 def runTest(frame
, nb
, log
): 
 206     win 
= TestValidatorPanel(nb
) 
 209 #---------------------------------------------------------------------- 
 220 wxValidator is the base class for a family of validator classes that mediate between a class of control, and application data. 
 222 A validator has three major roles: 
 224 1. to transfer data from a C++ variable or own storage to and from a control; 
 226 2. to validate data in a control, and show an appropriate error message; 
 228 3. to filter events (such as keystrokes), thereby changing the behaviour of the associated control. 
 230 Validators can be plugged into controls dynamically.