简体   繁体   English

尝试在Python中找到计时器

[英]Trying to find a timer in Python

I'm working on a Python program that is to use a timer. 我正在使用计时器的Python程序上工作。 It is going to be a memory game where a word pops up and a timer counts to 5 and after 5 seconds the words disappears and you have to type the word from memory. 这将是一个记忆游戏,其中弹出一个单词,计时器计数到5,5秒钟后单词消失,您必须从内存中键入单词。 I have already searched around a little and have seen things that time the execution time of a program but don't do anything that I want it to. 我已经进行了一些搜索,并且看到了一些时间,这些时间在程序的执行时间之外,但是却没有做我想要的任何事情。 Does anyone have a code to do such a thing? 有人有做这种事情的代码吗? I'm using Python 2.7.3 on a Windows Vista computer. 我在Windows Vista计算机上使用Python 2.7.3。

Thanks in advance, Sid 预先感谢,西德

time.sleep(seconds) will sleep for some seconds and you can print a message time.sleep(seconds)将休眠几秒钟,您可以打印一条消息

eg: 例如:

import time
for i in range(5,0,-1):
     print "%d Mins Left"%i
     time.sleep(60)

however if you want to update the previous time print you will need to look at the curses library (if you are using the terminal) or something like wxPython or pygame or any of the myriad of other graphical libraries for python if you want to use graphics 但是,如果您想更新以前的打印件,则需要查看curses库(如果使用的是终端)或wxPython或pygame之类的东西,或者如果要使用图形,则可以使用python的众多其他图形库中的任何一种

Use the time module: 使用时间模块:

from time import time
start = int(time())
topass = 5
while int(time()) - topass < start:
    pass
print topass, "seconds have passed"

If it is going to have a GUI, you could build this application easily using Tkinter using the after method of tkinter widgets. 如果要使用GUI,则可以使用Tkinter小部件的after方法使用Tkinter轻松构建此应用程序。

If you're not going to have a GUI, you'll probably need 2 threads (1 to do the timing, 1 to accept input from the keyboard). 如果您没有GUI,则可能需要2个线程(1个用于计时,1个用于接受键盘输入)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM