2017-05-02 17 views
0

私はPythonでtimerを実装したいと思いますが、それに関する有益な記事は見つかりませんでした。Pythonでtimerを実装する方法

`timer -> 60 seconds to zero 
    #do stuff 
    if the user selects to manually reset the timer -> start the time loop again 
    if user exits -> exit the time loop 
reset the timer and again do above steps` 

Amは上記のロジックを実装するための構文の記事/情報を探して: 私の主な目的は、次のロジックを実装することです。

+1

何ですかあなたは働いていますか?ユーザーは、端末、GUI、またはブラウザを介してプログラムとやりとりしていますか? – tiwo

+0

類似:http://stackoverflow.com/questions/10154568/postpone-code-for-later-execution-in-python-like-settimeout-in-javascript – tiwo

+0

'time.tme()'を使用しますか? –

答えて

0

ここにあなたの/ usr/binに/ストップウォッチでそれを入れて、適切な許可(chmodの+ Xスクリプト)を得

eg: sudo chmod +x /usr/bin/stopwatch 

はコード

#!/usr/bin/python 

from __future__ import print_function 

import sys 
import time 


if len(sys.argv) != 2: 
    print("Needs seconds as argument, Eg: stopwatch 10") 
else: 
    seconds = sys.argv[1] 
    for i in range(int(seconds), 0, -1): 
     print(str(" "*50), end='\r') 
     print(str(i), end='\r') 
     sys.stdout.flush() 
     time.sleep(1) 
    print("Time over {}".format(seconds)) 

使用

stopwatch 10 
0

通常、マルチスレッド環境でタイマを開発するには、stopitパッケージを使用します。 タイマーを定義することができます>ユーザーがキャンセルボタンを押した場合にタイマの.cancel()を呼び出す関数を作成し、タイマーを再起動するためのタイムアウト例外をキャッチします。

関連する問題