1

I'm coding a timer that counts minutes and seconds, and I want the timer to be able to be paused when you press enter - However, I'm using a while loop to make the timer actually count, and adding a raw_input/input would stop the loop... How would I make the two functions run dynamically - together but separate?

My code so far:

from datetime import datetime
import math
#import subprocess
import sys
import time
import os

sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=24, cols=60))

t1 = 0
t3 = 0

def space():
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")

while t1 != 1.1:

    os.system('clear')

    t2 = str(t1)
    t4 = str(t3)

    space()

    print('*')*60
    print "James Balazs' Python Timer".center(60,"-")
    print ("Passed time: " + t4 + " minutes and " + t2 + " seconds").center(60, " ")
    print('*')*60

    t1 = t1 + 1
    time.sleep(1)
    if t1 == 60:
        t1 = 0
        t3 = t3 + 1

Some unnecessary imports are because I am using a modified copy of a clock I made... So all help is much appreciated as long as I don't have to destroy the majority of my work to make this possible.

Matt
  • 74,352
  • 26
  • 153
  • 180

1 Answers1

0

Create a function with the timer and use threading to run it while asking for input. Then kill it when the user presses enter.

import threading
def timer():
    #Timer goes here
Thread(target = timer).start()
raw_input("Press enter to stop")
#Kill the thread

Also see http://mail.python.org/pipermail/python-list/2004-May/281943.html about killing.

Community
  • 1
  • 1
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75