Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a timer in python

Tags:

python

time

import time def timer():    now = time.localtime(time.time())    return now[5]   run = raw_input("Start? > ") while run == "start":    minutes = 0    current_sec = timer()    #print current_sec    if current_sec == 59:       mins = minutes + 1       print ">>>>>>>>>>>>>>>>>>>>>", mins 

I want to create a kind of stopwatch that when minutes reach 20 minutes, brings up a dialog box, The dialog box is not the problem. But my minutes variable does not increment in this code.

like image 743
user2711485 Avatar asked Aug 23 '13 15:08

user2711485


People also ask

Can I add a timer in Python?

If you're programming in Python, you're in luck: The language gives you tools to build your own timers. A timer program can be useful for not only monitoring your own time, but measuring how long your Python program takes to run.

How do you wait 5 seconds in Python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.


1 Answers

See Timer Objects from threading.

How about

from threading import Timer  def timeout():     print("Game over")  # duration is in seconds t = Timer(20 * 60, timeout) t.start()  # wait for time completion t.join() 

Should you want pass arguments to the timeout function, you can give them in the timer constructor:

def timeout(foo, bar=None):     print('The arguments were: foo: {}, bar: {}'.format(foo, bar))  t = Timer(20 * 60, timeout, args=['something'], kwargs={'bar': 'else'}) 

Or you can use functools.partial to create a bound function, or you can pass in an instance-bound method.

like image 196