File: PmwEntryField.py.html

package info (click to toggle)
python-pmw 0.6.2-0.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,652 kB
  • ctags: 2,716
  • sloc: python: 10,720; makefile: 44; sh: 24
file content (338 lines) | stat: -rw-r--r-- 13,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE HTML PUBLIC "-//Netscape_Microsoft//DTD HTML 3.0//EN">
<HTML>

<!-- This file generated using the Python HTMLgen module. -->
<HEAD>
  <META NAME="GENERATOR" CONTENT="HTMLgen 1.1">
        <TITLE>PmwEntryField.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Based on iwidgets2.2.0/entryfield.itk code.</FONT>

import regex
import types
import Tkinter
import Pmw

<FONT COLOR="#DD0000"># Entry field validation functions</FONT>

<STRONG>def _numeric</STRONG>(text):
    if text == <FONT COLOR="#009900">''</FONT>:
        return -1
    else:
	return _getRegex(<FONT COLOR="#009900">'^[0-9]*$'</FONT>).match(text) != -1
    
<STRONG>def _integer</STRONG>(text):
    if text in (<FONT COLOR="#009900">''</FONT>, <FONT COLOR="#009900">'-'</FONT>, <FONT COLOR="#009900">'+'</FONT>):
        return -1
    else:
	return _getRegex(<FONT COLOR="#009900">'^[-+]?\([0-9]*\)$'</FONT>).match(text) != -1
    
<STRONG>def _alphabetic</STRONG>(text):
    return _getRegex(<FONT COLOR="#009900">'^[a-z]*$'</FONT>, 1).match(text) != -1
    
<STRONG>def _alphanumeric</STRONG>(text):
    return _getRegex(<FONT COLOR="#009900">'^[0-9a-z]*$'</FONT>, 1).match(text) != -1
    
<STRONG>def _hexadecimal</STRONG>(text):
    if text in (<FONT COLOR="#009900">''</FONT>, <FONT COLOR="#009900">'0x'</FONT>, <FONT COLOR="#009900">'0X'</FONT>):
        return -1
    else:
	return _getRegex(<FONT COLOR="#009900">'^\(0x\)?\([0-9a-f]*\)$'</FONT>, 1).match(text) != -1
    
<STRONG>def _real</STRONG>(text):
    if text in (<FONT COLOR="#009900">''</FONT>, <FONT COLOR="#009900">'-'</FONT>, <FONT COLOR="#009900">'+'</FONT>, <FONT COLOR="#009900">'.'</FONT>, <FONT COLOR="#009900">'-.'</FONT>, <FONT COLOR="#009900">'+.'</FONT>,):
        return -1
    elif _getRegex(<FONT COLOR="#009900">'^[^e]*[0-9]'</FONT>).search(text) == -1:
        return 0
    else:
	return _getRegex(
	    <FONT COLOR="#009900">'^[-+]?[0-9]*\.?[0-9]*\([eE][-+]?[0-9]*\)?$'</FONT>).match(text) != -1
    
_from0to23 = <FONT COLOR="#009900">'\([0-9]\|[0-1][0-9]\|2[0-3]\)'</FONT>
_from0to60 = <FONT COLOR="#009900">'\([0-9]\|[0-5][0-9]\)'</FONT>
_time24Reg = <FONT COLOR="#009900">'^'</FONT> + _from0to23 + <FONT COLOR="#009900">':'</FONT> + _from0to60 + <FONT COLOR="#009900">':'</FONT> + _from0to60 + <FONT COLOR="#009900">'$'</FONT>
<STRONG>def _time24</STRONG>(text):
    if _getRegex(_time24Reg).match(text) != -1:
	return 1
    else:
	return -1
    
_timeNReg = <FONT COLOR="#009900">'^[-+]?[0-9]+:'</FONT> + _from0to60 + <FONT COLOR="#009900">':'</FONT> + _from0to60 + <FONT COLOR="#009900">'$'</FONT>
<STRONG>def _timeN</STRONG>(text):
    if _getRegex(_timeNReg).match(text) != -1:
	return 1
    else:
	return -1
    
_from1to31 = <FONT COLOR="#009900">'\(0?[1-9]\|[1-2][0-9]\|3[01]\)'</FONT>
_from1to12 = <FONT COLOR="#009900">'\(0?[1-9]\|1[0-2]\)'</FONT>
_digits = <FONT COLOR="#009900">'[0-9]+'</FONT>
_date_dmyReg = <FONT COLOR="#009900">'^'</FONT> + _from1to31 + <FONT COLOR="#009900">'/'</FONT> + _from1to12 + <FONT COLOR="#009900">'/'</FONT> + _digits + <FONT COLOR="#009900">'$'</FONT>
_date_mdyReg = <FONT COLOR="#009900">'^'</FONT> + _from1to12 + <FONT COLOR="#009900">'/'</FONT> + _from1to31 + <FONT COLOR="#009900">'/'</FONT> + _digits + <FONT COLOR="#009900">'$'</FONT>
_date_ymdReg = <FONT COLOR="#009900">'^'</FONT> + _digits + <FONT COLOR="#009900">'/'</FONT> + _from1to12 + <FONT COLOR="#009900">'/'</FONT> + _from1to31 + <FONT COLOR="#009900">'$'</FONT>
<STRONG>def _date_dmy</STRONG>(text):
    if _getRegex(_date_dmyReg).match(text) != -1:
	return 1
    else:
	return -1
    
<STRONG>def _date_mdy</STRONG>(text):
    if _getRegex(_date_mdyReg).match(text) != -1:
	return 1
    else:
	return -1
    
<STRONG>def _date_ymd</STRONG>(text):
    if _getRegex(_date_ymdReg).match(text) != -1:
	return 1
    else:
	return -1
    
_validators = {
    <FONT COLOR="#009900">'numeric'</FONT> : _numeric,            # integer >= 0
    <FONT COLOR="#009900">'integer'</FONT> : _integer,            # any integer
    <FONT COLOR="#009900">'hexadecimal'</FONT> : _hexadecimal,    # hex number (optionally with leading 0x)
    <FONT COLOR="#009900">'real'</FONT> : _real,                  # number with or without a decimal point
    <FONT COLOR="#009900">'alphabetic'</FONT> : _alphabetic,      # letters a-zA-Z
    <FONT COLOR="#009900">'alphanumeric'</FONT> : _alphanumeric,  # letters a-zA-Z and digits
    <FONT COLOR="#009900">'timeN'</FONT>    : _timeN,             # HH:MM:SS
    <FONT COLOR="#009900">'time24'</FONT>   : _time24,            # HH:MM:SS (between 00:00:00 and 23:59:59)
    <FONT COLOR="#009900">'date_dmy'</FONT> : _date_dmy,          # DD/MM/YY
    <FONT COLOR="#009900">'date_mdy'</FONT> : _date_mdy,          # MM/DD/YY
    <FONT COLOR="#009900">'date_ymd'</FONT> : _date_ymd,          # YY/MM/DD
}

_regexCache = {}

<STRONG>def _getRegex</STRONG>(pattern, ignoreCase = 0):
    global _regexCache
    item = pattern, ignoreCase
    if not _regexCache.has_key(item):
	if ignoreCase:
	    _regexCache[item] = regex.compile(pattern, regex.casefold)
	else:
	    _regexCache[item] = regex.compile(pattern)
    
    return _regexCache[item]

_entryCache = {}

<STRONG>def _registerEntryField</STRONG>(entry, entryField):
    <FONT COLOR="#DD0000"># Register an EntryField widget for an Entry widget</FONT>

    _entryCache[entry] = entryField

<STRONG>def _preProcess</STRONG>(event, *args):
    <FONT COLOR="#DD0000"># Forward preprocess events for an Entry to it's EntryField</FONT>

    _entryCache[event.widget]._preProcess()

<STRONG>def _postProcess</STRONG>(event, *args):
    <FONT COLOR="#DD0000"># Forward postprocess events for an Entry to it's EntryField</FONT>

    _entryCache[event.widget]._postProcess()

<STRONG><FONT COLOR="#CC6600">class EntryField</FONT></STRONG>(Pmw.MegaWidget):
    _classBindingsDefined = 0

<STRONG>    def __init__</STRONG>(self, parent = None, **kw):

	<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
	INITOPT = Pmw.INITOPT
	optiondefs = (
	    (<FONT COLOR="#009900">'command'</FONT>,           None,        None),
	    (<FONT COLOR="#009900">'errorbackground'</FONT>,   <FONT COLOR="#009900">'pink'</FONT>,      None),
	    (<FONT COLOR="#009900">'invalidcommand'</FONT>,    self.bell,   None),
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,       0,           INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,          None,        INITOPT),
	    (<FONT COLOR="#009900">'maxwidth'</FONT>,          0,           self._maxwidth),
	    (<FONT COLOR="#009900">'modifiedcommand'</FONT>,   None,        None),
	    (<FONT COLOR="#009900">'validate'</FONT>,          None,        self._validate),
	    (<FONT COLOR="#009900">'value'</FONT>,             <FONT COLOR="#009900">''</FONT>,          INITOPT),
	)
	self.defineoptions(kw, optiondefs)

	<FONT COLOR="#DD0000"># Initialise the base class (after defining the options).</FONT>
	Pmw.MegaWidget.__init__(self, parent)

	<FONT COLOR="#DD0000"># Create the components.</FONT>
	interior = self.interior()
	self._entryFieldEntry = self.createcomponent(<FONT COLOR="#009900">'entry'</FONT>,
		(), None,
		Tkinter.Entry, (interior,))
	self._entryFieldEntry.grid(column=2, row=2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	self._entryFieldEntry.insert(0, self[<FONT COLOR="#009900">'value'</FONT>])
	interior.grid_columnconfigure(2, weight=1)
	interior.grid_rowconfigure(2, weight=1)

	self.createlabel(interior)

	<FONT COLOR="#DD0000"># Initialise instance variables.</FONT>

	self.normalBackground = None
	self._validator = None
        self._previousText = None

	<FONT COLOR="#DD0000"># Initialise instance.</FONT>

	_registerEntryField(self._entryFieldEntry, self)

	<FONT COLOR="#DD0000"># establish the special class bindings if not already done</FONT>
	if not EntryField._classBindingsDefined:
	    tagList = self._entryFieldEntry.bindtags()
	    	    
	    allSequences = {}
	    for tag in tagList:

		<FONT COLOR="#DD0000"># bind_class returns a string, not a tuple</FONT>
		tk = self._hull.tk
	        sequences = tk.splitlist(self.bind_class(tag))

		for sequence in sequences:
		    allSequences[sequence] = None
	    for sequence in allSequences.keys():
		self.bind_class(<FONT COLOR="#009900">'EntryFieldPre'</FONT>, sequence, _preProcess)
		self.bind_class(<FONT COLOR="#009900">'EntryFieldPost'</FONT>, sequence, _postProcess)

	    EntryField._classBindingsDefined = 1

	self._entryFieldEntry.bindtags((<FONT COLOR="#009900">'EntryFieldPre'</FONT>,) +
		self._entryFieldEntry.bindtags() + (<FONT COLOR="#009900">'EntryFieldPost'</FONT>,))
	self._entryFieldEntry.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self._executeCommand)

	<FONT COLOR="#DD0000"># Check keywords and initialise options.</FONT>
	self.initialiseoptions(EntryField)

<STRONG>    def _maxwidth</STRONG>(self):
	maxwidth = self[<FONT COLOR="#009900">'maxwidth'</FONT>]
	if type(maxwidth) != types.IntType or maxwidth < 0:
	    raise ValueError, <FONT COLOR="#009900">'"maxwidth" option should be non-negative integer'</FONT>
        self._previousText = None
	self._checkValidity()

<STRONG>    def _validate</STRONG>(self):
	val = self[<FONT COLOR="#009900">'validate'</FONT>]
	if _validators.has_key(val):
	    self._validator = _validators[val]
	elif callable(val):
	    self._validator = val
	elif not val:
	    self._validator = None
	else:
	    validValues = _validators.keys()
	    validValues.sort()
	    raise ValueError, \
	        <FONT COLOR="#009900">'bad validate value "%s":  must be a function or one of %s'</FONT> \
		    % (val, validValues)
        self._previousText = None
	self._checkValidity()

<STRONG>    def _executeCommand</STRONG>(self, event):
	cmd = self[<FONT COLOR="#009900">'command'</FONT>]
	if callable(cmd):
	    # Let command return <FONT COLOR="#009900">'break'</FONT>.  This is useful if the
	    <FONT COLOR="#DD0000"># EntryField is in a Dialog and hitting Return in the</FONT>
	    <FONT COLOR="#DD0000"># EntryField should not invoke the Dialog's default</FONT>
	    <FONT COLOR="#DD0000"># button.</FONT>
	    return cmd()
	    
<STRONG>    def _preProcess</STRONG>(self):

        self._previousText = self._entryFieldEntry.get()
        self._previousICursor = self._entryFieldEntry.index(<FONT COLOR="#009900">'insert'</FONT>)
	if self._entryFieldEntry.selection_present():
	    self._previousSel= (self._entryFieldEntry.index(<FONT COLOR="#009900">'sel.first'</FONT>),
		self._entryFieldEntry.index(<FONT COLOR="#009900">'sel.last'</FONT>))
	else:
	    self._previousSel = None

<STRONG>    def _postProcess</STRONG>(self):

	<FONT COLOR="#DD0000"># No need to check if text has not changed.</FONT>
	previousText = self._previousText
	if previousText == self._entryFieldEntry.get():
	    return self.valid()

	valid = self._checkValidity()
	cmd = self[<FONT COLOR="#009900">'modifiedcommand'</FONT>]
	if callable(cmd) and previousText != self._entryFieldEntry.get():
	    cmd()
	return valid
	    
<STRONG>    def _getValidity</STRONG>(self):
	input = self._entryFieldEntry.get()
	if self[<FONT COLOR="#009900">'maxwidth'</FONT>] and len(input) > self[<FONT COLOR="#009900">'maxwidth'</FONT>]:
	    return 0
	elif self._validator:
	    return self._validator(input)
	else:
	    return 1

<STRONG>    def _checkValidity</STRONG>(self):
	valid = self._getValidity()
	oldValidity = valid

	if valid == 0:
	    <FONT COLOR="#DD0000"># The entry is invalid.</FONT>
	    cmd = self[<FONT COLOR="#009900">'invalidcommand'</FONT>]
	    if callable(cmd):
		cmd()

	    <FONT COLOR="#DD0000"># Restore the entry to its previous value.</FONT>
	    if self._previousText is not None:
		self.__setEntry(self._previousText)
		self._entryFieldEntry.icursor(self._previousICursor)
		if self._previousSel is not None:
		    self._entryFieldEntry.selection_range(self._previousSel[0],
			self._previousSel[1])

		<FONT COLOR="#DD0000"># Check if the saved text is valid as well.</FONT>
		valid = self._getValidity()

	self._valid = valid

	if valid == 1:
	    if self.normalBackground is not None:
		self._entryFieldEntry.configure(
			background = self.normalBackground)
		self.normalBackground = None
	else:
	    if self.normalBackground is None:
		self.normalBackground = self._entryFieldEntry.cget(<FONT COLOR="#009900">'background'</FONT>)
		self._entryFieldEntry.configure(
			background = self[<FONT COLOR="#009900">'errorbackground'</FONT>])

        return oldValidity

<STRONG>    def invoke</STRONG>(self):
	cmd = self[<FONT COLOR="#009900">'command'</FONT>]
	if callable(cmd):
	    cmd()

<STRONG>    def valid</STRONG>(self):
        return self._valid == 1

<STRONG>    def clear</STRONG>(self):
        self._entryFieldEntry.delete(0, <FONT COLOR="#009900">'end'</FONT>)

<STRONG>    def __setEntry</STRONG>(self, text):
	disabled = (self._entryFieldEntry.cget(<FONT COLOR="#009900">'state'</FONT>) == <FONT COLOR="#009900">'disabled'</FONT>)
	if disabled:
	    self._entryFieldEntry.configure(state=<FONT COLOR="#009900">'normal'</FONT>)
	self._entryFieldEntry.delete(0, <FONT COLOR="#009900">'end'</FONT>)
	self._entryFieldEntry.insert(0, text)
	if disabled:
	    self._entryFieldEntry.configure(state=<FONT COLOR="#009900">'disabled'</FONT>)

<STRONG>    def setentry</STRONG>(self, text):
	self._preProcess()
        self.__setEntry(text)
	return self._postProcess()

Pmw.forwardmethods(EntryField, Tkinter.Entry, <FONT COLOR="#009900">'_entryFieldEntry'</FONT>)

</PRE>

 </BODY> </HTML>