]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-09/validator1.py
4 The validator used in this example will ensure that the text
5 controls are not empty when you press the Ok button, and
6 will not let you leave if any of the Validations fail."""
9 class NotEmptyValidator(wx
.PyValidator
):
11 wx
.PyValidator
.__init
__(self
)
15 Note that every validator must implement the Clone() method.
17 return NotEmptyValidator()
19 def Validate(self
, win
):
20 textCtrl
= self
.GetWindow()
21 text
= textCtrl
.GetValue()
24 wx
.MessageBox("This field must contain some text!", "Error")
25 textCtrl
.SetBackgroundColour("pink")
30 textCtrl
.SetBackgroundColour(
31 wx
.SystemSettings_GetColour(wx
.SYS_COLOUR_WINDOW
))
35 def TransferToWindow(self
):
38 def TransferFromWindow(self
):
43 class MyDialog(wx
.Dialog
):
45 wx
.Dialog
.__init
__(self
, None, -1, "Validators: validating")
47 # Create the text controls
48 about
= wx
.StaticText(self
, -1, about_txt
)
49 name_l
= wx
.StaticText(self
, -1, "Name:")
50 email_l
= wx
.StaticText(self
, -1, "Email:")
51 phone_l
= wx
.StaticText(self
, -1, "Phone:")
53 name_t
= wx
.TextCtrl(self
, validator
=NotEmptyValidator())
54 email_t
= wx
.TextCtrl(self
, validator
=NotEmptyValidator())
55 phone_t
= wx
.TextCtrl(self
, validator
=NotEmptyValidator())
57 # Use standard button IDs
58 okay
= wx
.Button(self
, wx
.ID_OK
)
60 cancel
= wx
.Button(self
, wx
.ID_CANCEL
)
63 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
64 sizer
.Add(about
, 0, wx
.ALL
, 5)
65 sizer
.Add(wx
.StaticLine(self
), 0, wx
.EXPAND|wx
.ALL
, 5)
67 fgs
= wx
.FlexGridSizer(3, 2, 5, 5)
68 fgs
.Add(name_l
, 0, wx
.ALIGN_RIGHT
)
69 fgs
.Add(name_t
, 0, wx
.EXPAND
)
70 fgs
.Add(email_l
, 0, wx
.ALIGN_RIGHT
)
71 fgs
.Add(email_t
, 0, wx
.EXPAND
)
72 fgs
.Add(phone_l
, 0, wx
.ALIGN_RIGHT
)
73 fgs
.Add(phone_t
, 0, wx
.EXPAND
)
75 sizer
.Add(fgs
, 0, wx
.EXPAND|wx
.ALL
, 5)
77 btns
= wx
.StdDialogButtonSizer()
79 btns
.AddButton(cancel
)
81 sizer
.Add(btns
, 0, wx
.EXPAND|wx
.ALL
, 5)
87 app
= wx
.PySimpleApp()