簡體   English   中英

在python中創建一個計時器

[英]Creating a timer in python

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

我想創建一種秒表,當分鍾達到 20 分鍾時,會彈出一個對話框,對話框不是問題。 但是我的分鍾變量在這段代碼中沒有增加。

請參閱線程中的 計時器對象

怎么樣

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()

如果您想將參數傳遞給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'})

或者你可以使用functools.partial創建一個綁定函數,或者你可以傳入一個實例綁定方法。

您可以使用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

我會使用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

您的代碼很完美,只是您必須進行以下替換:

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

要么

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

但這里是這個問題的另一種解決方案:

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

應該

minutes = minutes + 1

還,

minutes = 0

需要在while循環之外。

我想創建一種秒表,當分鍾達到 20 分鍾時,會彈出一個對話框

您所需要的只是在指定的時間睡覺。 time.sleep() 需要幾秒鍾才能進入睡眠狀態,因此 20 * 60 是 20 分鍾。

import time
run = raw_input("Start? > ")
time.sleep(20 * 60)
your_code_to_bring_up_dialog_box()
# 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 
import time 

...

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

...
import time
mintt=input("How many seconds you want to time?:")
timer=int(mintt)
while (timer != 0 ):
    timer=timer-1
    time.sleep(1)
    print(timer)

這項工作非常好以計時秒。

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

嘗試讓你的 while 循環像這樣:

minutes = 0

while run == "start":
   current_sec = timer()
   #print current_sec
   if current_sec == 59:
      minutes = minutes + 1
      print ">>>>>>>>>>>>>>>>>>>>>", mins
import time
def timer(n):
    while n!=0:
        n=n-1
        time.sleep(n)#time.sleep(seconds) #here you can mention seconds according to your requirement.
        print "00 : ",n
timer(30) #here you can change n according to your requirement.
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

實際上,我自己正在尋找一個計時器,而您的代碼似乎可以正常工作,您的分鍾數未被計算的可能原因是,當您這么說時

分鍾 = 0

接着

分鍾 = 分鍾 + 1

這和說一樣

分鍾 = 0 + 1

我敢打賭,每次打印 mins 時,它都會顯示“1”,因為我剛剛解釋過,“0+1”總是會導致“1”。

你首先要做的是把你的

分鍾 = 0

在 while 循環之外的聲明。 之后,您可以刪除

分鍾 = 分鍾 + 1

行,因為在這種情況下您實際上並不需要另一個變量,只需將其替換為

分鍾 = 分鍾 + 1

這樣,分鍾將從值“0”開始,接收新值“0+1”,接收新值“1+1”,接收新值“2+1”等。

我意識到很多人已經回答了它,但我認為它會更有幫助,學習明智,如果你能看到你犯了錯誤的地方並嘗試修復它。希望它有所幫助。 另外,謝謝你的計時器。

from datetime import datetime
now=datetime.now()
Sec=-1
sec=now.strftime("%S")
SEC=0
while True:
    SEC=int(SEC)
    sec=int(sec)
    now=datetime.now()
    sec=now.strftime("%S")
    if SEC<sec:
        Sec+=1
    SEC=sec
print(Sec) #the timer is Sec

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM