【发布时间】:2016-10-02 11:26:10
【问题描述】:
我最近开始编写一个 Python 程序,如下面的片段所示。
# 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 的时间和日期时间模块的信息,试图弄清楚如何使用它们来实现我的目标,但是我尝试实现它们却没有用。
基本上,我需要我的程序来测量从按下按钮开始经过的时间,并能够以小时:分钟.秒的格式显示它。减法使其变得非常困难,必须实现诸如 timedelta 之类的东西。根据我在网上阅读的内容,如果没有 datetime 模块,就无法做我想做的事,但它给我的只是问题。
是否有更简单的解决方案,我的代码是否有任何未解决的错误,我有多愚蠢?
【问题讨论】:
-
您可以使用
time.time来测量经过的时间。
标签: python datetime time timer