]>
Commit | Line | Data |
---|---|---|
9e46ac75 | 1 | from wxPython.wx import * |
9e46ac75 RD |
2 | |
3 | ||
4 | class floatValidator(wxPyValidator): | |
5 | ||
6 | def __init__(self, obj=None, attrName=""): | |
7 | wxPyValidator.__init__(self) | |
8 | self.numList = ['1','2','3','4','5','6','7','8','9','0','.'] | |
9 | EVT_CHAR(self, self.OnChar) | |
10 | self.obj = obj | |
11 | self.attrName = attrName | |
12 | ||
13 | def Clone(self): | |
14 | return floatValidator(self.obj, self.attrName) | |
15 | ||
16 | def TransferToWindow(self): | |
17 | if self.obj and hasattr(self.obj, self.attrName): | |
18 | tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl") | |
19 | tc.SetValue(str(getattr(self.obj, self.attrName))) | |
20 | return true | |
21 | ||
22 | def TransferFromWindow(self): | |
23 | if self.obj and self.attrName: | |
24 | tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl") | |
25 | text = tc.GetValue() | |
1e4a197e | 26 | setattr(self.obj, self.attrName, float(text)) |
9e46ac75 RD |
27 | return true |
28 | ||
29 | ||
30 | def Validate(self, win): | |
31 | tc = wxPyTypeCast(self.GetWindow(), "wxTextCtrl") | |
32 | val = tc.GetValue() | |
33 | ||
34 | for x in val: | |
35 | if x not in self.numList: | |
36 | return false | |
37 | ||
38 | return true | |
39 | ||
40 | def OnChar(self, event): | |
41 | key = event.KeyCode() | |
42 | if key < WXK_SPACE or key == WXK_DELETE or key > 255: | |
43 | event.Skip() | |
44 | return | |
45 | ||
46 | if chr(key) in self.numList: | |
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 MyDialog(wxDialog): | |
60 | def __init__(self, parent): | |
61 | wxDialog.__init__(self, parent, -1, "hello") | |
62 | ||
63 | self.theValue = 555.12 | |
64 | fltValidator = floatValidator(self, "theValue") | |
65 | ||
66 | Vbox = wxBoxSizer(wxVERTICAL) | |
67 | Tbox = wxBoxSizer(wxHORIZONTAL) | |
68 | Tbox.Add(wxStaticText(self, -1, "Initial Balance"), 0, wxALL,5) | |
69 | Tbox.Add(wxTextCtrl(self, 13, "123.45", validator = fltValidator, | |
70 | size=(100, -1)), 0, wxALL,5) | |
71 | ||
72 | Vbox.Add(Tbox, 0, 0) | |
73 | ||
74 | Tbox = wxBoxSizer(wxHORIZONTAL) | |
75 | Tbox.Add(wxButton(self, wxID_OK, "Ok"), 0, wxALL,5) | |
76 | Tbox.Add(wxButton(self, wxID_CANCEL, "Cancel"), 0, wxALL,5) | |
77 | ||
78 | Vbox.Add(Tbox, 0, 0) | |
79 | ||
80 | self.SetAutoLayout(true) | |
81 | self.SetSizer(Vbox) | |
82 | Vbox.Fit(self) | |
83 | ||
84 | ||
85 | class TestFrame(wxFrame): | |
86 | def __init__(self, parent): | |
87 | wxFrame.__init__(self, parent, -1, "Testing...", size=(150,75)) | |
88 | wxButton(self, 25, "Click Me") | |
89 | EVT_BUTTON(self, 25, self.OnClick) | |
90 | ||
91 | ||
92 | def OnClick(self, event): | |
93 | dlg = MyDialog(self) | |
94 | dlg.ShowModal() | |
95 | print dlg.theValue | |
96 | dlg.Destroy() | |
97 | ||
98 | ||
99 | ||
100 | app = wxPySimpleApp() | |
101 | frame = TestFrame(None) | |
102 | frame.Show(true) | |
103 | app.MainLoop() | |
104 | ||
105 | ||
106 | ||
107 |