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
|
# Authors: Joe VanAndel and Greg McFarlane
import string
import sys
import time
import Tkinter
import Pmw
class TimeCounter(Pmw.MegaWidget):
"""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.
"""
def __init__(self, parent = None, **kw):
# Define the megawidget options.
INITOPT = Pmw.INITOPT
optiondefs = (
('autorepeat', 1, None),
('buttonaspect', 1.0, INITOPT),
('command', None, None),
('initwait', 300, None),
('labelmargin', 0, INITOPT),
('labelpos', None, INITOPT),
('max', None, self._max),
('min', None, self._min),
('padx', 0, INITOPT),
('pady', 0, INITOPT),
('repeatrate', 50, None),
('value', None, INITOPT),
)
self.defineoptions(kw, optiondefs)
# Initialise the base class (after defining the options).
Pmw.MegaWidget.__init__(self, parent)
self.arrowDirection = {}
self._flag = 'stopped'
self._timerId = None
self._createComponents(kw)
value = self['value']
if value is None:
now = time.time()
value = time.strftime('%H:%M:%S', time.localtime(now))
self.setvalue(value)
# Check keywords and initialise options.
self.initialiseoptions()
def _createComponents(self, kw):
# Create the components.
interior = self.interior()
# If there is no label, put the arrows and the entry directly
# into the interior, otherwise create a frame for them. In
# either case the border around the arrows and the entry will
# be raised (but not around the label).
if self['labelpos'] is None:
frame = interior
if not kw.has_key('hull_relief'):
frame.configure(relief = 'raised')
if not kw.has_key('hull_borderwidth'):
frame.configure(borderwidth = 1)
else:
frame = self.createcomponent('frame',
(), None,
Tkinter.Frame, (interior,),
relief = 'raised', borderwidth = 1)
frame.grid(column=2, row=2, sticky='nsew')
interior.grid_columnconfigure(2, weight=1)
interior.grid_rowconfigure(2, weight=1)
# Create the down arrow buttons.
# Create the hour down arrow.
self._downHourArrowBtn = self.createcomponent('downhourarrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._downHourArrowBtn] = 'down'
self._downHourArrowBtn.grid(column = 0, row = 2)
# Create the minute down arrow.
self._downMinuteArrowBtn = self.createcomponent('downminutearrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._downMinuteArrowBtn] = 'down'
self._downMinuteArrowBtn.grid(column = 1, row = 2)
# Create the second down arrow.
self._downSecondArrowBtn = self.createcomponent('downsecondarrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._downSecondArrowBtn] = 'down'
self._downSecondArrowBtn.grid(column = 2, row = 2)
# Create the entry fields.
# Create the hour entry field.
self._hourCounterEntry = self.createcomponent('hourentryfield',
(('hourentry', 'hourentryfield_entry'),), None,
Pmw.EntryField, (frame,), validate='integer', entry_width = 2)
self._hourCounterEntry.grid(column = 0, row = 1, sticky = 'news')
# Create the minute entry field.
self._minuteCounterEntry = self.createcomponent('minuteentryfield',
(('minuteentry', 'minuteentryfield_entry'),), None,
Pmw.EntryField, (frame,), validate='integer', entry_width = 2)
self._minuteCounterEntry.grid(column = 1, row = 1, sticky = 'news')
# Create the second entry field.
self._secondCounterEntry = self.createcomponent('secondentryfield',
(('secondentry', 'secondentryfield_entry'),), None,
Pmw.EntryField, (frame,), validate='integer', entry_width = 2)
self._secondCounterEntry.grid(column = 2, row = 1, sticky = 'news')
# Create the up arrow buttons.
# Create the hour up arrow.
self._upHourArrowBtn = self.createcomponent('uphourarrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._upHourArrowBtn] = 'up'
self._upHourArrowBtn.grid(column = 0, row = 0)
# Create the minute up arrow.
self._upMinuteArrowBtn = self.createcomponent('upminutearrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._upMinuteArrowBtn] = 'up'
self._upMinuteArrowBtn.grid(column = 1, row = 0)
# Create the second up arrow.
self._upSecondArrowBtn = self.createcomponent('upsecondarrow',
(), 'Arrow',
Tkinter.Canvas, (frame,),
width = 16, height = 16, relief = 'raised', borderwidth = 2)
self.arrowDirection[self._upSecondArrowBtn] = 'up'
self._upSecondArrowBtn.grid(column = 2, row = 0)
# Make it resize nicely.
padx = self['padx']
pady = self['pady']
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)
# Create the label.
self.createlabel(interior)
# Set bindings.
# Up hour
self._upHourArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._upHourArrowBtn:
s._drawArrow(button, 'up'))
self._upHourArrowBtn.bind('<1>',
lambda event, s=self,button=self._upHourArrowBtn:
s._countUp(button, 3600))
self._upHourArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._upHourArrowBtn:
s._stopUpDown(button))
# Up minute
self._upMinuteArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._upMinuteArrowBtn:
s._drawArrow(button, 'up'))
self._upMinuteArrowBtn.bind('<1>',
lambda event, s=self,button=self._upMinuteArrowBtn:
s._countUp(button, 60))
self._upMinuteArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._upMinuteArrowBtn:
s._stopUpDown(button))
# Up second
self._upSecondArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._upSecondArrowBtn:
s._drawArrow(button, 'up'))
self._upSecondArrowBtn.bind('<1>',
lambda event, s=self,button=self._upSecondArrowBtn:
s._countUp(button, 1))
self._upSecondArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._upSecondArrowBtn:
s._stopUpDown(button))
# Down hour
self._downHourArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._downHourArrowBtn:
s._drawArrow(button, 'down'))
self._downHourArrowBtn.bind('<1>',
lambda event, s=self,button=self._downHourArrowBtn:
s._countDown(button, 3600))
self._downHourArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._downHourArrowBtn:
s._stopUpDown(button))
# Down minute
self._downMinuteArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._downMinuteArrowBtn:
s._drawArrow(button, 'down'))
self._downMinuteArrowBtn.bind('<1>',
lambda event, s=self,button=self._downMinuteArrowBtn:
s._countDown(button, 60))
self._downMinuteArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._downMinuteArrowBtn:
s._stopUpDown(button))
# Down second
self._downSecondArrowBtn.bind('<Configure>',
lambda event, s=self,button=self._downSecondArrowBtn:
s._drawArrow(button, 'down'))
self._downSecondArrowBtn.bind('<1>',
lambda event, s=self, button=self._downSecondArrowBtn:
s._countDown(button,1))
self._downSecondArrowBtn.bind('<Any-ButtonRelease-1>',
lambda event, s=self, button=self._downSecondArrowBtn:
s._stopUpDown(button))
self._hourCounterEntry.component('entry').bind(
'<Return>', self._invoke)
self._minuteCounterEntry.component('entry').bind(
'<Return>', self._invoke)
self._secondCounterEntry.component('entry').bind(
'<Return>', self._invoke)
self._hourCounterEntry.bind('<Configure>', self._resizeArrow)
self._minuteCounterEntry.bind('<Configure>', self._resizeArrow)
self._secondCounterEntry.bind('<Configure>', self._resizeArrow)
def _drawArrow(self, arrow, direction):
Pmw.drawarrow(arrow, self['hourentry_foreground'], direction, 'arrow')
def _resizeArrow(self, event = None):
for btn in (self._upHourArrowBtn, self._upMinuteArrowBtn,
self._upSecondArrowBtn,
self._downHourArrowBtn,
self._downMinuteArrowBtn, self._downSecondArrowBtn):
bw = (string.atoi(btn['borderwidth']) +
string.atoi(btn['highlightthickness']))
newHeight = self._hourCounterEntry.winfo_reqheight() - 2 * bw
newWidth = int(newHeight * self['buttonaspect'])
btn.configure(width=newWidth, height=newHeight)
self._drawArrow(btn, self.arrowDirection[btn])
def _min(self):
min = self['min']
if min is None:
self._minVal = 0
else:
self._minVal = Pmw.timestringtoseconds(min)
def _max(self):
max = self['max']
if max is None:
self._maxVal = None
else:
self._maxVal = Pmw.timestringtoseconds(max)
def getvalue(self):
return self.getstring()
def setvalue(self, text):
list = string.split(text, ':')
if len(list) != 3:
raise ValueError, 'invalid value: ' + text
self._hour = string.atoi(list[0])
self._minute = string.atoi(list[1])
self._second = string.atoi(list[2])
self._setHMS()
def getstring(self):
return '%02d:%02d:%02d' % (self._hour, self._minute, self._second)
def getint(self):
return self._hour * 3600 + self._minute * 60 + self._second
def _countUp(self, button, increment):
self._relief = self._upHourArrowBtn.cget('relief')
button.configure(relief='sunken')
self._count(1, 'start', increment)
def _countDown(self, button, increment):
self._relief = self._downHourArrowBtn.cget('relief')
button.configure(relief='sunken')
self._count(-1, 'start', increment)
def increment(self, seconds = 1):
self._count(1, 'force', seconds)
def decrement(self, seconds = 1):
self._count(-1, 'force', seconds)
def _count(self, factor, newFlag = None, increment = 1):
if newFlag != 'force':
if newFlag is not None:
self._flag = newFlag
if self._flag == 'stopped':
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 != 'force':
if self['autorepeat']:
if self._flag == 'start':
delay = self['initwait']
self._flag = 'running'
else:
delay = self['repeatrate']
self._timerId = self.after(
delay, lambda self=self, factor=factor,increment=increment:
self._count(factor,'running', increment))
def _setHMS(self):
self._hourCounterEntry.setentry('%02d' % self._hour)
self._minuteCounterEntry.setentry('%02d' % self._minute)
self._secondCounterEntry.setentry('%02d' % self._second)
def _stopUpDown(self, button):
if self._timerId is not None:
self.after_cancel(self._timerId)
self._timerId = None
button.configure(relief=self._relief)
self._flag = 'stopped'
def _invoke(self, event):
cmd = self['command']
if callable(cmd):
cmd()
def invoke(self):
cmd = self['command']
if callable(cmd):
return cmd()
def destroy(self):
if self._timerId is not None:
self.after_cancel(self._timerId)
self._timerId = None
Pmw.MegaWidget.destroy(self)
|