Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | title = 'Subclassing Pmw.EntryField' |
2 | ||
3 | # Import Pmw from this directory tree. | |
4 | import sys | |
5 | sys.path[:0] = ['../../..'] | |
6 | ||
7 | import string | |
8 | import time | |
9 | import types | |
10 | import Tkinter | |
11 | import Pmw | |
12 | ||
13 | class SpecialEntry(Pmw.EntryField): | |
14 | ||
15 | def __init__(self, parent=None , **kw): | |
16 | ||
17 | kw['extravalidators'] = _myValidators | |
18 | apply(Pmw.EntryField.__init__, (self, parent), kw) | |
19 | self._converter = None | |
20 | ||
21 | def setentry(self, text): | |
22 | # Override Pmw.EntryField.setentry to pass string through | |
23 | # the appropriate converter. | |
24 | ||
25 | val = self['validate'] | |
26 | if type(val) == types.DictionaryType: | |
27 | val = val['validator'] | |
28 | if _converters.has_key(val): | |
29 | text = _converters[val](text, output = 0) | |
30 | Pmw.EntryField.setentry(self, text) | |
31 | ||
32 | def getentry(self): | |
33 | text = self.get() | |
34 | val = self['validate'] | |
35 | if type(val) == types.DictionaryType: | |
36 | val = val['validator'] | |
37 | if _converters.has_key(val): | |
38 | return _converters[val](text, output = 1) | |
39 | else: | |
40 | return text | |
41 | ||
42 | def _date(text): | |
43 | return Pmw.datevalidator(text, 'dmy', '.') | |
44 | ||
45 | def _real(text): | |
46 | return Pmw.realvalidator(text, ',') | |
47 | ||
48 | def _dateconv(text, output = 0): | |
49 | # On output, convert from dd.mm.yy to mm-dd-yy. On input, convert | |
50 | # mm-dd-yy to dd.mm.yy and also from +NN+ or -NN- to date NN days | |
51 | # before or after today. | |
52 | ||
53 | if len(text) == 0: | |
54 | return '' | |
55 | if output: | |
56 | try: | |
57 | d = string.split(text, '.') | |
58 | return d[1] + '-' + d[0] + '-' + d[2] | |
59 | except: | |
60 | return text | |
61 | else: | |
62 | if text[-1] == '+' or text[-1] == '-': | |
63 | text = text[:-1] | |
64 | if text[0] == '+' or text[0] == '-': | |
65 | secondsAhead = string.atoi(text) * 3600 * 24 | |
66 | return time.strftime('%d.%m.%Y', | |
67 | time.localtime(time.time() + secondsAhead)) | |
68 | try: | |
69 | d = string.split(text,'-') | |
70 | return d[1] + '.' + d[0] + '.' + d[2] | |
71 | except: | |
72 | return text | |
73 | ||
74 | def _realconv(text, output = 0): | |
75 | # Convert between DD.DD and DD,DD. | |
76 | ||
77 | if output: | |
78 | index = string.find(text, ',') | |
79 | if index >= 0: | |
80 | return text[:index] + '.' + text[index + 1:] | |
81 | else: | |
82 | return text | |
83 | else: | |
84 | index = string.find(text, '.') | |
85 | if index >= 0: | |
86 | return text[:index] + ',' + text[index + 1:] | |
87 | else: | |
88 | return text | |
89 | ||
90 | ||
91 | _converters = { | |
92 | 'real' : _realconv, | |
93 | 'float8' : _realconv, | |
94 | 'date' : _dateconv | |
95 | } | |
96 | ||
97 | _myValidators = { | |
98 | 'date' : (_date, lambda s: Pmw.datestringtojdn(s, 'dmy', '.')), | |
99 | 'real' : (_real, lambda s: string.atof(_realconv(s, 1))), | |
100 | 'int4' : ('numeric', 'numeric'), | |
101 | 'oid' : ('int4', 'int4'), | |
102 | 'float8' : ('real', 'real'), | |
103 | 'varchar' : ('alphanumeric', 'alphanumeric'), | |
104 | 'text' : ('alphanumeric', 'alphanumeric'), | |
105 | } | |
106 | ||
107 | class Demo: | |
108 | def __init__(self, parent): | |
109 | # Create and pack the SpecialEntry megawidgets. | |
110 | self._any = SpecialEntry(parent, | |
111 | labelpos = 'w', | |
112 | label_text = 'Text (max 10 chars):', | |
113 | validate = {'validator' : 'text', 'max' : 10}, | |
114 | command = self.execute) | |
115 | self._any.setentry('abc') | |
116 | self._int = SpecialEntry(parent, | |
117 | labelpos = 'w', | |
118 | label_text = 'Int4:', | |
119 | validate = 'int4') | |
120 | self._int.setentry(1) | |
121 | self._real = SpecialEntry(parent, | |
122 | labelpos = 'w', | |
123 | label_text = 'Real (max 2,5e+9):', | |
124 | validate = {'validator' : 'real', 'max' : +2.5e+9}, | |
125 | ) | |
126 | self._real.setentry('+2.5e+6') | |
127 | self._date = SpecialEntry(parent, | |
128 | labelpos = 'w', | |
129 | label_text = 'Date (dd.mm.yy):', | |
130 | validate = 'date', | |
131 | modifiedcommand = self.changed | |
132 | ) | |
133 | # Set entry to one week from now, using special intput format. | |
134 | self._date.setentry('+7+') | |
135 | ||
136 | entries = (self._any, self._int, self._real, self._date) | |
137 | ||
138 | for entry in entries: | |
139 | entry.pack(fill='x', expand=1, padx=10, pady=5) | |
140 | Pmw.alignlabels(entries) | |
141 | ||
142 | self._any.component('entry').focus_set() | |
143 | ||
144 | def changed(self): | |
145 | print 'Text changed, converted value is', self._date.getentry() | |
146 | ||
147 | def execute(self): | |
148 | print 'Return pressed, value is', self._any.get() | |
149 | ||
150 | # This implements a custom validation routine. It simply checks | |
151 | # if the string is of odd length. | |
152 | def custom_validate(self, text): | |
153 | print 'text:', text | |
154 | if len(text) % 2 == 0: | |
155 | return -1 | |
156 | else: | |
157 | return 1 | |
158 | ||
159 | ###################################################################### | |
160 | ||
161 | # Create demo in root window for testing. | |
162 | if __name__ == '__main__': | |
163 | root = Tkinter.Tk() | |
164 | Pmw.initialise(root) | |
165 | root.title(title) | |
166 | ||
167 | exitButton = Tkinter.Button(root, text = 'Exit', command = root.destroy) | |
168 | exitButton.pack(side = 'bottom') | |
169 | widget = Demo(root) | |
170 | root.mainloop() |