0

I'm attempting to create a stopwatch-like gui application (wxpython). I have the buttons: start, stop, reset, and a frame that displays 00:00.0 (mm:ss:tt, minutes, seconds, tenths of seconds). However, I'm stumped at trying to get the correct output using integers. I would like this output for example:

...
...
...
TICK = 0
t_format = u"%02d:%02d.%02d" % (min, sec, t_sec)

...t_format(0) -> 0:00.0
...t_format(12) -> 0.01.2
...t_format(321) -> 0:32.1

...
...
...

while (self.stop != True) or (self.reset != True):
    t_format(TICK)

    TICK += 1

...
...
...
suffa
  • 3,606
  • 8
  • 46
  • 66

1 Answers1

2

Use integer division and modulo to turn tenths of seconds into minutes, seconds, and tenths:

def t_format(tt):
    sec = tt / 10
    return '%02d:%02d.%01d' % (sec / 60, sec % 60, tt % 10)

However, your code needs to be careful to sleep approximately one tenths of a second between each tick. For example:

TICK += 1
to_sleep = (start_time + TICK / 10.0) - time.time()
if to_sleep > 0:
    time.sleep(to_sleep)
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • last_tick_time += 0.1 would make the clock more accurate than last_tick_time = now – Scintillo May 13 '13 at 13:48
  • @Scintillo Good point. An even better option is to avoid incrementing `last_tick_time` at all, and use the existing `TICK` counter to compute the sleep time. – user4815162342 May 13 '13 at 14:09