X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f6bcfd974ef26faf6f91a62cac09827e09463fd1..f65a69e9a01cfa41129cd7ba12d92b00f6c6aa1d:/wxPython/demo/wxValidator.py diff --git a/wxPython/demo/wxValidator.py b/wxPython/demo/wxValidator.py index 4217494c6b..fc1300229e 100644 --- a/wxPython/demo/wxValidator.py +++ b/wxPython/demo/wxValidator.py @@ -19,20 +19,19 @@ class MyValidator(wxPyValidator): return MyValidator(self.flag) def Validate(self, win): - print 'validate' - tc = wxPyTypeCast(win, "wxTextCtrl") + tc = self.GetWindow() val = tc.GetValue() if self.flag == ALPHA_ONLY: for x in val: if x not in string.letters: - return false + return False elif self.flag == DIGIT_ONLY: for x in val: if x not in string.digits: - return false + return False - return true + return True def OnChar(self, event): @@ -59,7 +58,7 @@ class MyValidator(wxPyValidator): class TestValidatorPanel(wxPanel): def __init__(self, parent): wxPanel.__init__(self, parent, -1) - self.SetAutoLayout(true) + self.SetAutoLayout(True) VSPACE = 10 fgs = wxFlexGridSizer(0, 2) @@ -81,26 +80,134 @@ class TestValidatorPanel(wxPanel): fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER) fgs.Add(wxTextCtrl(self, -1, "", validator = MyValidator(DIGIT_ONLY))) + fgs.Add(1,VSPACE); fgs.Add(1,VSPACE) + fgs.Add(1,VSPACE); fgs.Add(1,VSPACE) + fgs.Add(0,0) + b = wxButton(self, -1, "Test Dialog Validation") + EVT_BUTTON(self, b.GetId(), self.OnDoDialog) + fgs.Add(b) + border = wxBoxSizer() border.Add(fgs, 1, wxGROW|wxALL, 25) self.SetSizer(border) self.Layout() + def OnDoDialog(self, evt): + dlg = TestValidateDialog(self) + dlg.ShowModal() + dlg.Destroy() + #---------------------------------------------------------------------- -def runTest(frame, nb, log): - win = TestValidatorPanel(nb) - return win +class TextObjectValidator(wxPyValidator): + """ This validator is used to ensure that the user has entered something + into the text object editor dialog's text field. + """ + def __init__(self): + """ Standard constructor. + """ + wxPyValidator.__init__(self) + + + + def Clone(self): + """ Standard cloner. + + Note that every validator must implement the Clone() method. + """ + return TextObjectValidator() + + + def Validate(self, win): + """ Validate the contents of the given text control. + """ + textCtrl = self.GetWindow() + text = textCtrl.GetValue() + + if len(text) == 0: + wxMessageBox("A text object must contain some text!", "Error") + textCtrl.SetBackgroundColour("pink") + textCtrl.SetFocus() + textCtrl.Refresh() + return False + else: + textCtrl.SetBackgroundColour( + wxSystemSettings_GetColour(wxSYS_COLOUR_WINDOW)) + textCtrl.Refresh() + return True + + + def TransferToWindow(self): + """ Transfer data from validator to window. + + The default implementation returns False, indicating that an error + occurred. We simply return True, as we don't do any data transfer. + """ + return True # Prevent wxDialog from complaining. + + + def TransferFromWindow(self): + """ Transfer data from window to validator. + + The default implementation returns False, indicating that an error + occurred. We simply return True, as we don't do any data transfer. + """ + return True # Prevent wxDialog from complaining. #---------------------------------------------------------------------- +class TestValidateDialog(wxDialog): + def __init__(self, parent): + wxDialog.__init__(self, parent, -1, "Validated Dialog") + + self.SetAutoLayout(True) + VSPACE = 10 + + fgs = wxFlexGridSizer(0, 2) + + fgs.Add(1,1); + fgs.Add(wxStaticText(self, -1, + "These controls must have text entered into them. Each\n" + "one has a validator that is checked when the Okay\n" + "button is clicked.")) + fgs.Add(1,VSPACE); fgs.Add(1,VSPACE) + label = wxStaticText(self, -1, "First: ") + fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER) + fgs.Add(wxTextCtrl(self, -1, "", validator = TextObjectValidator())) + fgs.Add(1,VSPACE); fgs.Add(1,VSPACE) + + label = wxStaticText(self, -1, "Second: ") + fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER) + fgs.Add(wxTextCtrl(self, -1, "", validator = TextObjectValidator())) + buttons = wxBoxSizer(wxHORIZONTAL) + b = wxButton(self, wxID_OK, "Okay") + b.SetDefault() + buttons.Add(b, 0, wxALL, 10) + buttons.Add(wxButton(self, wxID_CANCEL, "Cancel"), 0, wxALL, 10) + + border = wxBoxSizer(wxVERTICAL) + border.Add(fgs, 1, wxGROW|wxALL, 25) + border.Add(buttons) + self.SetSizer(border) + border.Fit(self) + self.Layout() + + +#---------------------------------------------------------------------- + +def runTest(frame, nb, log): + win = TestValidatorPanel(nb) + return win + +#---------------------------------------------------------------------- + overview = """\ @@ -117,3 +224,11 @@ A validator has three major roles: Validators can be plugged into controls dynamically. """ + + + +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])]) +