如何在用户输入之前运行python循环?定时器格式

时间:2013-04-23 21:15:54

标签: python timer

我正在编写一个计算分钟和秒钟的计时器,我想按下回车键时可以暂停计时器 - 但是,我正在使用while循环来使计时器实际计数,并添加一个raw_input / input会停止循环...我如何使两个函数动态运行 - 一起但是分开?

到目前为止我的代码:

from datetime import datetime
import math
#import subprocess
import sys
import time
import os

sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=24, cols=60))

t1 = 0
t3 = 0

def space():
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")
    print(" ")

while t1 != 1.1:

    os.system('clear')

    t2 = str(t1)
    t4 = str(t3)

    space()

    print('*')*60
    print "James Balazs' Python Timer".center(60,"-")
    print ("Passed time: " + t4 + " minutes and " + t2 + " seconds").center(60, " ")
    print('*')*60

    t1 = t1 + 1
    time.sleep(1)
    if t1 == 60:
        t1 = 0
        t3 = t3 + 1

一些不必要的导入是因为我正在使用我制作的时钟的修改副本......所以只要我不必破坏我的大部分工作来实现这一点,所有的帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

使用计时器创建一个函数,并在请求输入时使用threading运行它。然后当用户按下回车时kill it

import threading
def timer():
    #Timer goes here
Thread(target = timer).start()
raw_input("Press enter to stop")
#Kill the thread

另见http://mail.python.org/pipermail/python-list/2004-May/281943.html关于杀人。

相关问题