【问题标题】:How to make a pausable timer in python?如何在python中制作一个可暂停的计时器?
【发布时间】:2020-05-18 10:41:57
【问题描述】:

我想在 python 中创建一个计时器,具有以下功能:

timer.start() - 应该启动计时器

timer.pause() - 应该暂停计时器

timer.resume() - 应该恢复计时器

timer.get() - 应该返回当前时间

计时器应该从 0 向上运行。它是用来测量时间的,而不是触发回调函数。

因此,如果您启动它,它应该像 0 1 2 3 一样开始计算秒数,如果您暂停它,它应该仍然是 3,但不会更进一步。恢复后,它会继续 4 5 6 等等

我该怎么做?


Pause/Resume functions for timer 不是重复的,因为我不关心回调。

【问题讨论】:

  • 您的问题涉及计时器。我不明白您需要的内容与链接的帖子之间应该有什么区别。也许edit您的问题并添加相关信息?
  • 我编辑了它,但由于它已关闭,我将不得不制作一个新的:stackoverflow.com/questions/60027221/…
  • 请不要重复您的问题。如果编辑足够,您的问题将被重新打开。
  • 到目前为止你尝试过什么?你想让计时器真正count(例如不断地打印到屏幕上)还是只在ˋtimer.get()ˋ上提供时间?您只想要秒精度(如计数所暗示的那样)还是计算机时钟的精度?

标签: python python-3.x time


【解决方案1】:
# mytimer.py
from datetime import datetime
import time

class MyTimer():
    """
    timer.start() - should start the timer
    timer.pause() - should pause the timer
    timer.resume() - should resume the timer
    timer.get() - should return the current time
    """

    def __init__(self):
        print('Initializing timer')
        self.timestarted = None
        self.timepaused = None
        self.paused = False

    def start(self):
        """ Starts an internal timer by recording the current time """
        print("Starting timer")
        self.timestarted = datetime.now()

    def pause(self):
        """ Pauses the timer """
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if self.paused:
            raise ValueError("Timer is already paused")
        print('Pausing timer')
        self.timepaused = datetime.now()
        self.paused = True

    def resume(self):
        """ Resumes the timer by adding the pause time to the start time """
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if not self.paused:
            raise ValueError("Timer is not paused")
        print('Resuming timer')
        pausetime = datetime.now() - self.timepaused
        self.timestarted = self.timestarted + pausetime
        self.paused = False

    def get(self):
        """ Returns a timedelta object showing the amount of time
            elapsed since the start time, less any pauses """
        print('Get timer value')
        if self.timestarted is None:
            raise ValueError("Timer not started")
        if self.paused:
            return self.timepaused - self.timestarted
        else:
            return datetime.now() - self.timestarted

if __name__ == "__main__":
    t = MyTimer()
    t.start()
    print('Waiting 2 seconds'); time.sleep(2)
    print(t.get())
    print('Waiting 1 second'); time.sleep(1)
    t.pause()
    print('Waiting 2 seconds [paused]'); time.sleep(2)
    print(t.get())
    print('Waiting 1 second [paused]'); time.sleep(1)
    print(t.get())
    print('Waiting 1 second [paused]'); time.sleep(1)
    t.resume()
    print('Waiting 1 second'); time.sleep(1)
    print(t.get())

运行

python mytimer.py

输出

初始化定时器 启动计时器 等待 2 秒 获取定时器值 0:00:02.001523 等待 1 秒 暂停计时器 等待 2 秒 [暂停] 获取定时器值 0:00:03.004724 等待 1 秒 [暂停] 获取定时器值 0:00:03.004724 等待 1 秒 [暂停] 恢复定时器 等待 1 秒 获取定时器值 0:00:04.008578

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多