在python中创建一个计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18406165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:39:37  来源:igfitidea点击:

Creating a timer in python

pythontime

提问by user2711485

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.

我想创建一种秒表,当分钟达到 20 分钟时,会弹出一个对话框,对话框不是问题。但是我的分钟变量在这段代码中没有增加。

采纳答案by Antti Haapala

You can really simplify this whole program by using time.sleep:

您可以使用time.sleep以下方法真正简化整个程序:

import time
run = raw_input("Start? > ")
mins = 0
# Only run if the user types in "start"
if run == "start":
    # Loop until we reach 20 minutes running
    while mins != 20:
        print(">>>>>>>>>>>>>>>>>>>>> {}".format(mins))
        # Sleep for a minute
        time.sleep(60)
        # Increment the minute total
        mins += 1
    # Bring up the dialog box here

回答by David

mins = minutes + 1

should be

应该

minutes = minutes + 1

Also,

还,

minutes = 0

needs to be outside of the while loop.

需要在while循环之外。

回答by lmjohns3

You're probably looking for a Timer object: http://docs.python.org/2/library/threading.html#timer-objects

您可能正在寻找 Timer 对象:http: //docs.python.org/2/library/threading.html#timer-objects

回答by m-oliv

Try having your while loop like this:

尝试让你的 while 循环像这样:

minutes = 0

while run == "start":
   current_sec = timer()
   #print current_sec
   if current_sec == 59:
      minutes = minutes + 1
      print ">>>>>>>>>>>>>>>>>>>>>", mins

回答by Antti Haapala

See Timer Objectsfrom 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 timeoutfunction, you can give them in the timer constructor:

如果要将参数传递给timeout函数,可以在计时器构造函数中提供它们:

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.partialto create a bound function, or you can pass in an instance-bound method.

或者你可以使用functools.partial来创建绑定函数,或者你可以传入一个实例绑定方法。

回答by Anshu Dwibhashi

Your code's perfect except that you must do the following replacement:

您的代码很完美,只是您必须进行以下替换:

minutes += 1 #instead of mins = minutes + 1

or

或者

minutes = minutes + 1 #instead of mins = minutes + 1

but here's another solution to this problem:

但这里是这个问题的另一种解决方案:

def wait(time_in_seconds):
    time.sleep(time_in_seconds) #here it would be 1200 seconds (20 mins)

回答by Silas Ray

I'd use a timedeltaobject.

我会使用一个timedelta对象。

from datetime import datetime, timedelta

...
period = timedelta(minutes=1)
next_time = datetime.now() + period
minutes = 0
while run == 'start':
    if next_time <= datetime.now():
        minutes += 1
        next_time += period

回答by tdelaney

I want to create a kind of stopwatch that when minutes reach 20 minutes, brings up a dialog box.

我想创建一种秒表,当分钟达到 20 分钟时,会弹出一个对话框

All you need is to sleep the specified time. time.sleep() takes seconds to sleep, so 20 * 60 is 20 minutes.

您所需要的只是在指定的时间睡觉。time.sleep() 需要几秒钟才能进入睡眠状态,因此 20 * 60 是 20 分钟。

import time
run = raw_input("Start? > ")
time.sleep(20 * 60)
your_code_to_bring_up_dialog_box()

回答by Karrar Ali

# this is kind of timer, stop after the input minute run out.    
import time
min=int(input('>>')) 
while min>0:
    print min
    time.sleep(60) # every minute 
    min-=1  # take one minute 

回答by tortue

import time 

...

def stopwatch(mins):
   # complete this whole code in some mins.
   time.sleep(60*mins)

...