]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxValidator.py
fixed an error in the sample
[wxWidgets.git] / utils / wxPython / demo / wxValidator.py
1
2 from wxPython.wx import *
3 from wxPython.lib.grids import wxFlexGridSizer
4
5 import string
6
7 #----------------------------------------------------------------------
8
9 ALPHA_ONLY = 1
10 DIGIT_ONLY = 2
11
12 class MyValidator(wxPyValidator):
13 def __init__(self, flag=None, pyVar=None):
14 wxPyValidator.__init__(self)
15 self.flag = flag
16 EVT_CHAR(self, self.OnChar)
17
18 def Clone(self):
19 return MyValidator(self.flag)
20
21 def Validate(self, win):
22 print 'validate'
23 tc = wxPyTypeCast(win, "wxTextCtrl")
24 val = tc.GetValue()
25 if self.flag == ALPHA_ONLY:
26 for x in val:
27 if x not in string.letters:
28 return false
29
30 elif self.flag == DIGIT_ONLY:
31 for x in val:
32 if x not in string.digits:
33 return false
34
35 return true
36
37
38 def OnChar(self, event):
39 key = event.KeyCode()
40 if key < WXK_SPACE or key == WXK_DELETE or key > 255:
41 event.Skip()
42 return
43 if self.flag == ALPHA_ONLY and chr(key) in string.letters:
44 event.Skip()
45 return
46 if self.flag == DIGIT_ONLY and chr(key) in string.digits:
47 event.Skip()
48 return
49
50 if not wxValidator_IsSilent():
51 wxBell()
52
53 # Returning without calling even.Skip eats the event before it
54 # gets to the text control
55 return
56
57 #----------------------------------------------------------------------
58
59 class TestValidatorPanel(wxPanel):
60 def __init__(self, parent):
61 wxPanel.__init__(self, parent, -1)
62 self.SetAutoLayout(true)
63 VSPACE = 10
64
65 fgs = wxFlexGridSizer(0, 2)
66
67 fgs.Add(1,1);
68 fgs.Add(wxStaticText(self, -1, "These controls have validators that limit\n"
69 "the type of characters that can be entered."))
70
71 fgs.Add(1,VSPACE); fgs.Add(1,VSPACE)
72
73 label = wxStaticText(self, -1, "Alpha Only: ")
74 fgs.Add(label, 0, wxALIGN_RIGHT|wxCENTER)
75
76 fgs.Add(wxTextCtrl(self, -1, "", validator = MyValidator(ALPHA_ONLY)))
77
78 fgs.Add(1,VSPACE); fgs.Add(1,VSPACE)
79
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)))
83
84 border = wxBoxSizer()
85 border.Add(fgs, 1, wxGROW|wxALL, 25)
86 self.SetSizer(border)
87 self.Layout()
88
89
90 #----------------------------------------------------------------------
91
92 def runTest(frame, nb, log):
93 win = TestValidatorPanel(nb)
94 return win
95
96 #----------------------------------------------------------------------
97
98
99
100
101
102
103
104
105
106 overview = """\
107 wxValidator is the base class for a family of validator classes that mediate between a class of control, and application data.
108
109 A validator has three major roles:
110
111 1. to transfer data from a C++ variable or own storage to and from a control;
112
113 2. to validate data in a control, and show an appropriate error message;
114
115 3. to filter events (such as keystrokes), thereby changing the behaviour of the associated control.
116
117 Validators can be plugged into controls dynamically.
118
119 """