2016-10-02 9 views
-1

私は最近、以下の部分で見られるように、Pythonプログラムの作業を開始しました。Pythonとtime/datetime

# General Variables 
running = False 
new = True 
timeStart = 0.0 
timeElapsed = 0.0 

def endProg(): 
    curses.nocbreak() 
    stdscr.keypad(False) 
    curses.echo() 
    curses.endwin() 
    quit() 

# Draw 
def draw(): 
    stdscr.addstr(1, 1, ">", curses.color_pair(6)) 
    stdscr.border() 
    if running: 
     stdscr.addstr(1, 3, t.strftime("%H:%M.%S", t.ctime(timeStart - timeElapsed))) 

     stdscr.redrawwin() 
    stdscr.refresh() 

# Calculate 
def calc(): 
    if running: 
     timeElapsed = t.clock() - timeStart 

stdscr.border() 
stdscr.addstr(1, 3, t.strftime("%H:%M.%S", t.gmtime(t.clock() - t.clock()))) 

# Main Loop 
while True: 
    # Get Input 
    kInput = stdscr.getch() 

    # Close the program 
    if kInput == ord('q'): 
     endProg() 

    # Stop the current run 
    elif kInput == ord('s'): 
     stdscr.addstr(1, 3, t.strftime("%H:%M.%S", t.gmtime(t.clock() - t.clock()))) 
     running = False 
     new = True 

    # Start a run 
    elif kInput == ord(' ') and new: 
     running = not running 
     new = not new 
     timeStart = dt.datetime.now() 

    # Toggle the timer 
    elif kInput == ord('p') and not new: 
     timeStart = dt.datetime.now() - timeStart 
     running = not running 

    calc() 
    draw() 

何かが右に見ていない場合は私のプログラムでは、ソリューション間のビット現在、申し訳ありませんです。私は説明することよりも喜んでいるでしょう。

Python用の時間と日時のモジュールについて、私は過去数時間をオンラインで読んでいました。目標を達成するためにどのように使うことができるのか理解しようとしましたが、実装しようとしましたが、 。

基本的には、ボタンが押されてからの経過時間を測定し、それを1時間:分秒の形式で表示できるようにするプログラムが必要です。減算は非常に困難になり、timedeltaのようなものを実装する必要がありました。私がオンラインで読んだことから、datetimeモジュールなしでは自分が望んでいることをする方法はありませんが、問題は何も与えません。

もっと簡単な解決法はありますか?私のコードには未解決のエラーがありますか?どのように愚かですか?

import time 

star = time.time() 
# run long processing... 
elapsed = time.time() - start 

のEtほら:'time.time`を使用して

+0

'time.time'を使って経過時間を測定できます。 –

答えて

0

0
def draw(): 
    stdscr.border() 
    if running: 
     stdscr.addstr(1, 1, ">", curses.color_pair(8)) 
     stdscr.addstr(1, 3, t.strftime("%H:%M.%S", t.gmtime(timeElapsed))) 

    if not running: 
     stdscr.addstr(1, 1, ">", curses.color_pair(7)) 

    stdscr.redrawwin() 
    stdscr.refresh() 

# Calculate 
def calc(): 
    if running: 
     timeElapsed = t.time() - timeStart 

stdscr.border() 
stdscr.addstr(1, 3, t.strftime("%H:%M.%S", t.gmtime(t.time() - t.time()))) 

# Main Loop 
while True: 
    # Get Input 
    kInput = stdscr.getch() 

    # Close the program 
    if kInput == ord('q'): 
     endProg() 

    # Stop the current run 
    elif kInput == ord('s'): 
     running = False 
     new = True 

    # Start a run 
    elif kInput == ord(' ') and new: 
     running = not running 
     new = not new 
     timeStart = t.time() 

    # Toggle the timer 
    elif kInput == ord('p') and not new: 
     running = not running 
     timeStart = t.time() - timeStart 

    calc() 
    draw() 

まあ、私は遭遇したすべてのエラーを取り除くことができました。ただし、表示される時刻が間違っています。

私たちは経過時間を探しているので、0から開始して増加する必要があります。私のプログラムは12:21.xxから始まり、増加しています。

私のコードを見るとエラーはありませんが、明らかにどこかにエラーがあります。