File: PmwTimeCounter.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 (401 lines) | stat: -rw-r--r-- 17,302 bytes parent folder | download | duplicates (2)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!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>PmwTimeCounter.py</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<PRE>
<FONT COLOR="#DD0000"># Authors: Joe VanAndel and Greg McFarlane</FONT>

import string
import sys
import time
import Tkinter
import Pmw

<STRONG><FONT COLOR="#CC6600">class TimeCounter</FONT></STRONG>(Pmw.MegaWidget):
    <FONT COLOR="#0000DD">"""Up-down counter

    A TimeCounter is a single-line entry widget with Up and Down arrows
    which increment and decrement the Time value in the entry.  
    """</FONT>

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

	<FONT COLOR="#DD0000"># Define the megawidget options.</FONT>
	INITOPT = Pmw.INITOPT
	optiondefs = (
	    (<FONT COLOR="#009900">'autorepeat'</FONT>,    1,    INITOPT),
	    (<FONT COLOR="#009900">'buttonaspect'</FONT>,  1.0,  INITOPT),
	    (<FONT COLOR="#009900">'initwait'</FONT>,      300,  INITOPT),
	    (<FONT COLOR="#009900">'labelmargin'</FONT>,   0,    INITOPT),
	    (<FONT COLOR="#009900">'labelpos'</FONT>,      None, INITOPT),
	    (<FONT COLOR="#009900">'max'</FONT>,           <FONT COLOR="#009900">''</FONT>,   self._max),
	    (<FONT COLOR="#009900">'min'</FONT>,           <FONT COLOR="#009900">''</FONT>,   self._min),
	    (<FONT COLOR="#009900">'padx'</FONT>,          0,    INITOPT),
	    (<FONT COLOR="#009900">'pady'</FONT>,          0,    INITOPT),
	    (<FONT COLOR="#009900">'repeatrate'</FONT>,    50,   INITOPT),
	    (<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)

    	self.arrowDirection = {}
	self._flag = <FONT COLOR="#009900">'stopped'</FONT>
	self._timerId = None

	self._createComponents()

	value = self[<FONT COLOR="#009900">'value'</FONT>]
	if value is None or value == <FONT COLOR="#009900">''</FONT>:
	    now = time.time()
	    value = time.strftime(<FONT COLOR="#009900">'%H:%M:%S'</FONT>,time.gmtime(now))
    	self._setTimeFromStr(value)

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

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

	<FONT COLOR="#DD0000"># Create the components.</FONT>
	interior = self.interior()

	<FONT COLOR="#DD0000"># If there is no label, put the arrows and the entry directly</FONT>
	<FONT COLOR="#DD0000"># into the interior, otherwise create a frame for them.  In</FONT>
	<FONT COLOR="#DD0000"># either case the border around the arrows and the entry will</FONT>
	<FONT COLOR="#DD0000"># be raised (but not around the label).</FONT>
	if self[<FONT COLOR="#009900">'labelpos'</FONT>] is None:
	    frame = interior
	else:
	    frame = self.createcomponent(<FONT COLOR="#009900">'frame'</FONT>,
		    (), None,
		    Tkinter.Frame, (interior,))
	    frame.grid(column=2, row=2, sticky=<FONT COLOR="#009900">'nsew'</FONT>)
	    interior.grid_columnconfigure(2, weight=1)
	    interior.grid_rowconfigure(2, weight=1)

	frame.configure(relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 1)

	<FONT COLOR="#DD0000"># Create the down arrow buttons.</FONT>

	<FONT COLOR="#DD0000"># Create the hour down arrow.</FONT>
	self._downHourArrowBtn = self.createcomponent(<FONT COLOR="#009900">'downhourarrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._downHourArrowBtn] = 0
	self._downHourArrowBtn.grid(column = 0, row = 2)

	<FONT COLOR="#DD0000"># Create the minute down arrow.</FONT>
	self._downMinuteArrowBtn = self.createcomponent(<FONT COLOR="#009900">'downminutearrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._downMinuteArrowBtn] = 0
	self._downMinuteArrowBtn.grid(column = 1, row = 2)

	<FONT COLOR="#DD0000"># Create the second down arrow.</FONT>
	self._downSecondArrowBtn = self.createcomponent(<FONT COLOR="#009900">'downsecondarrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._downSecondArrowBtn] = 0
	self._downSecondArrowBtn.grid(column = 2, row = 2)

	<FONT COLOR="#DD0000"># Create the entry fields.</FONT>

	<FONT COLOR="#DD0000"># Create the hour entry field.</FONT>
	self._hourCounterEntry = self.createcomponent(<FONT COLOR="#009900">'hourentryfield'</FONT>,
		((<FONT COLOR="#009900">'hourentry'</FONT>, <FONT COLOR="#009900">'hourentryfield_entry'</FONT>),), None,
		Pmw.EntryField, (frame,), validate=<FONT COLOR="#009900">'integer'</FONT>, entry_width = 2)
	self._hourCounterEntry.grid(column = 0, row = 1, sticky = <FONT COLOR="#009900">'news'</FONT>)

	<FONT COLOR="#DD0000"># Create the minute entry field.</FONT>
	self._minuteCounterEntry = self.createcomponent(<FONT COLOR="#009900">'minuteentryfield'</FONT>,
		((<FONT COLOR="#009900">'minuteentry'</FONT>, <FONT COLOR="#009900">'minuteentryfield_entry'</FONT>),), None,
		Pmw.EntryField, (frame,), validate=<FONT COLOR="#009900">'integer'</FONT>, entry_width = 2)
	self._minuteCounterEntry.grid(column = 1, row = 1, sticky = <FONT COLOR="#009900">'news'</FONT>)

	<FONT COLOR="#DD0000"># Create the second entry field.</FONT>
	self._secondCounterEntry = self.createcomponent(<FONT COLOR="#009900">'secondentryfield'</FONT>,
		((<FONT COLOR="#009900">'secondentry'</FONT>, <FONT COLOR="#009900">'secondentryfield_entry'</FONT>),), None,
		Pmw.EntryField, (frame,), validate=<FONT COLOR="#009900">'integer'</FONT>, entry_width = 2)
	self._secondCounterEntry.grid(column = 2, row = 1, sticky = <FONT COLOR="#009900">'news'</FONT>)

	<FONT COLOR="#DD0000"># Create the up arrow buttons.</FONT>

	<FONT COLOR="#DD0000"># Create the hour up arrow.</FONT>
	self._upHourArrowBtn = self.createcomponent(<FONT COLOR="#009900">'uphourarrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._upHourArrowBtn] = 1
	self._upHourArrowBtn.grid(column = 0, row = 0)

	<FONT COLOR="#DD0000"># Create the minute up arrow.</FONT>
	self._upMinuteArrowBtn = self.createcomponent(<FONT COLOR="#009900">'upminutearrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._upMinuteArrowBtn] = 1
	self._upMinuteArrowBtn.grid(column = 1, row = 0)

	<FONT COLOR="#DD0000"># Create the second up arrow.</FONT>
	self._upSecondArrowBtn = self.createcomponent(<FONT COLOR="#009900">'upsecondarrow'</FONT>,
		(), <FONT COLOR="#009900">'Arrow'</FONT>,
		Tkinter.Canvas, (frame,),
		width = 16, height = 16, relief = <FONT COLOR="#009900">'raised'</FONT>, borderwidth = 2)
    	self.arrowDirection[self._upSecondArrowBtn] = 1
	self._upSecondArrowBtn.grid(column = 2, row = 0)

	<FONT COLOR="#DD0000"># Make it resize nicely.</FONT>
	padx = self[<FONT COLOR="#009900">'padx'</FONT>]
	pady = self[<FONT COLOR="#009900">'pady'</FONT>]
	for col in range(3):
	    frame.grid_columnconfigure(col, weight = 1, pad = padx)
	frame.grid_rowconfigure(0, pad = pady)
	frame.grid_rowconfigure(2, pad = pady)

	frame.grid_rowconfigure(1, weight = 1)

	<FONT COLOR="#DD0000"># Create the label.</FONT>
	self.createlabel(interior)

	<FONT COLOR="#DD0000"># Set bindings.</FONT>

	<FONT COLOR="#DD0000"># Up hour</FONT>
	self._upHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._upHourArrowBtn: 
		s._drawArrow(button, 1))

	self._upHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self,button=self._upHourArrowBtn: 
		s._countUp(button, 3600))

	self._upHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._upHourArrowBtn:
		s._stopUpDown(button))

	<FONT COLOR="#DD0000"># Up minute</FONT>
	self._upMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._upMinuteArrowBtn: 
		s._drawArrow(button, 1))
	    

	self._upMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self,button=self._upMinuteArrowBtn: 
		s._countUp(button, 60))

	self._upMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._upMinuteArrowBtn:
		s._stopUpDown(button))

	<FONT COLOR="#DD0000"># Up second</FONT>
	self._upSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._upSecondArrowBtn: 
		s._drawArrow(button, 1))
	    

	self._upSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self,button=self._upSecondArrowBtn: 
		s._countUp(button, 1))

	self._upSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._upSecondArrowBtn:
		s._stopUpDown(button))

	<FONT COLOR="#DD0000"># Down hour</FONT>
	self._downHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._downHourArrowBtn: 
		s._drawArrow(button, 0))

	self._downHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self,button=self._downHourArrowBtn: 
		s._countDown(button, 3600))
	self._downHourArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._downHourArrowBtn:
		s._stopUpDown(button))


	<FONT COLOR="#DD0000"># Down minute</FONT>
	self._downMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._downMinuteArrowBtn: 
		s._drawArrow(button, 0))

	self._downMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self,button=self._downMinuteArrowBtn: s._countDown(button, 60))
	self._downMinuteArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._downMinuteArrowBtn:
		s._stopUpDown(button))

	<FONT COLOR="#DD0000"># Down second</FONT>
	self._downSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, 
		lambda  event, s=self,button=self._downSecondArrowBtn: 
		s._drawArrow(button, 0))

	self._downSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;1&gt;'</FONT>, 
    	    	lambda event, s=self, button=self._downSecondArrowBtn: 
		s._countDown(button,1))
	self._downSecondArrowBtn.bind(<FONT COLOR="#009900">'&lt;Any-ButtonRelease-1&gt;'</FONT>, 
		lambda event, s=self, button=self._downSecondArrowBtn:
		s._stopUpDown(button))

	self._hourCounterEntry.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self.invoke)
	self._minuteCounterEntry.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self.invoke)
	self._secondCounterEntry.bind(<FONT COLOR="#009900">'&lt;Return&gt;'</FONT>, self.invoke)

	self._hourCounterEntry.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, self._resizeArrow)
	self._minuteCounterEntry.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, self._resizeArrow)
	self._secondCounterEntry.bind(<FONT COLOR="#009900">'&lt;Configure&gt;'</FONT>, self._resizeArrow)

<STRONG>    def _drawArrow</STRONG>(self, arrow, direction):
	arrow.delete(<FONT COLOR="#009900">'arrow'</FONT>)

	fg = self._hourCounterEntry.cget(<FONT COLOR="#009900">'entry_foreground'</FONT>)

	bw = (string.atoi(arrow[<FONT COLOR="#009900">'borderwidth'</FONT>]) +
		string.atoi(arrow[<FONT COLOR="#009900">'highlightthickness'</FONT>])) / 2
	h = string.atoi(arrow[<FONT COLOR="#009900">'height'</FONT>]) + 2 * bw
	w =  string.atoi(arrow[<FONT COLOR="#009900">'width'</FONT>]) + 2 * bw

	if direction == 0:
    	     <FONT COLOR="#DD0000"># down arrow</FONT>
	     arrow.create_polygon(
		 0.25 * w + bw, 0.25 * h + bw,
	         0.50 * w + bw, 0.75 * h + bw,
	         0.75 * w + bw, 0.25 * h + bw,
		 fill=fg, tag=<FONT COLOR="#009900">'arrow'</FONT>)
	else:
	     arrow.create_polygon(
	         0.25 * w + bw, 0.75 * h + bw,
		 0.50 * w + bw, 0.25 * h + bw,
	         0.75 * w + bw, 0.75 * h + bw,
		 fill=fg, tag=<FONT COLOR="#009900">'arrow'</FONT>)

<STRONG>    def _resizeArrow</STRONG>(self, event = None):
	for btn in (self._upHourArrowBtn, self._upMinuteArrowBtn,
		self._upSecondArrowBtn,
		self._downHourArrowBtn,
		self._downMinuteArrowBtn, self._downSecondArrowBtn):
	    bw = (string.atoi(btn[<FONT COLOR="#009900">'borderwidth'</FONT>]) + \
		    string.atoi(btn[<FONT COLOR="#009900">'highlightthickness'</FONT>]))
	    newHeight = self._hourCounterEntry.winfo_reqheight() - 2 * bw
	    newWidth = newHeight * self[<FONT COLOR="#009900">'buttonaspect'</FONT>]
	    btn.configure(width=newWidth, height=newHeight)
	    self._drawArrow(btn, self.arrowDirection[btn])

<STRONG>    def _min</STRONG>(self):
	min = self[<FONT COLOR="#009900">'min'</FONT>]
        if min == <FONT COLOR="#009900">''</FONT>:
	    self._minVal = 0
	else:
	    self._minVal = Pmw.timestringtoseconds(min)

<STRONG>    def _max</STRONG>(self):
	max = self[<FONT COLOR="#009900">'max'</FONT>]
	if max != <FONT COLOR="#009900">''</FONT>:
	    self._maxVal = Pmw.timestringtoseconds(max)
	else:
	    self._maxVal = None


<STRONG>    def _setTimeFromStr</STRONG>(self, str):
        list = string.split(str, <FONT COLOR="#009900">':'</FONT>)
	if len(list) != 3:
	    raise TypeError, <FONT COLOR="#009900">'invalid value: '</FONT> + str

	self._hour = string.atoi(list[0])
	self._minute = string.atoi(list[1])
	self._second = string.atoi(list[2]) 

    	self._setHMS()

<STRONG>    def getstring</STRONG>(self):
    	return <FONT COLOR="#009900">'%02d:%02d:%02d'</FONT> % (self._hour, self._minute, self._second)

<STRONG>    def getint</STRONG>(self):
    	return self._hour * 3600 + self._minute * 60 + self._second

<STRONG>    def _countUp</STRONG>(self, button, increment):
	self._relief = self._upHourArrowBtn.cget(<FONT COLOR="#009900">'relief'</FONT>)
	button.configure(relief=<FONT COLOR="#009900">'sunken'</FONT>)
	self._count(1, <FONT COLOR="#009900">'start'</FONT>, increment)

<STRONG>    def _countDown</STRONG>(self, button, increment):

	self._relief = self._downHourArrowBtn.cget(<FONT COLOR="#009900">'relief'</FONT>)
	button.configure(relief=<FONT COLOR="#009900">'sunken'</FONT>)
	self._count(-1, <FONT COLOR="#009900">'start'</FONT>, increment)

<STRONG>    def increment</STRONG>(self):
	self._count(1, <FONT COLOR="#009900">'force'</FONT>)

<STRONG>    def decrement</STRONG>(self):
	self._count(-1, <FONT COLOR="#009900">'force'</FONT>)

<STRONG>    def _count</STRONG>(self, factor, newFlag=None,increment=1):
	if newFlag != <FONT COLOR="#009900">'force'</FONT>:
	  if newFlag is not None:
	    self._flag = newFlag

	  if self._flag == <FONT COLOR="#009900">'stopped'</FONT>:
	    return

	value = (string.atoi(self._hourCounterEntry.get()) *3600) + \
	      (string.atoi(self._minuteCounterEntry.get()) *60) + \
	      string.atoi(self._secondCounterEntry.get()) + \
	      factor * increment
	min = self._minVal
	max = self._maxVal
	if value < min:
	  value = min
	if max is not None and value > max:
	  value = max

	self._hour = value /3600
	self._minute = (value - (self._hour*3600)) / 60
	self._second = value - (self._hour*3600) - (self._minute*60)
	self._setHMS()

	if newFlag != <FONT COLOR="#009900">'force'</FONT>:
	  if self[<FONT COLOR="#009900">'autorepeat'</FONT>]:
	    if self._flag == <FONT COLOR="#009900">'start'</FONT>:
	      delay = self[<FONT COLOR="#009900">'initwait'</FONT>]
	      self._flag = <FONT COLOR="#009900">'running'</FONT>
	    else:
	      delay = self[<FONT COLOR="#009900">'repeatrate'</FONT>]
	    self._timerId = self.after(
		delay, lambda self=self, factor=factor,increment=increment: 
		  self._count(factor,<FONT COLOR="#009900">'running'</FONT>, increment))

<STRONG>    def _setHMS</STRONG>(self):
        self._hourCounterEntry.setentry(<FONT COLOR="#009900">'%02d'</FONT> % self._hour)
        self._minuteCounterEntry.setentry(<FONT COLOR="#009900">'%02d'</FONT> % self._minute)
        self._secondCounterEntry.setentry(<FONT COLOR="#009900">'%02d'</FONT> % self._second)

<STRONG>    def _stopUpDown</STRONG>(self, button):
        if self._timerId is not None:
            self.after_cancel(self._timerId)
	    self._timerId = None
        button.configure(relief=self._relief)
        self._flag = <FONT COLOR="#009900">'stopped'</FONT>

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

<STRONG>    def destroy</STRONG>(self):
        if self._timerId is not None:
            self.after_cancel(self._timerId)
	    self._timerId = None
        Pmw.MegaWidget.destroy(self)

</PRE>

 </BODY> </HTML>