简体   繁体   English

具有停止暂停和继续功能而不使用时间模块的Python计时器

[英]Python timer with Stop Pause and Continue without using time module

How to create Python timer with Stop, Pause and Continue functions without using "time" module, especially time.sleep() because my programm freezes after this function? 如何在不使用“时间”模块的情况下使用Stop,Pause和Continue函数创建Python计时器,尤其是time.sleep(),因为我的程序在该函数之后冻结了? Thank you 谢谢

To be more specific what i am trying to acchieve , I want to create an inapplication script that counts for how long was the programm opened. 具体来说,我想创建一个应用程序脚本,该脚本计算程序打开的时间。 If application was minimized - timer has to pause. 如果应用程序已最小化-计时器必须暂停。 In this step, using time.sleep() freezes my application If application is closed - timer has to stop 在此步骤中,使用time.sleep()冻结我的应用程序如果应用程序已关闭-计时器必须停止

If I've correctly undestood your problem, you want something to count how long the application is opened, and pausing all the time the application is minimized. 如果我正确地解决了您的问题,那么您需要一些信息来计算应用程序打开的时间,并在应用程序最小化的过程中一直暂停。 For that, you do not need to use any sleep . 为此,您无需进行任何sleep You simply react to events, noting the time the event occurs and cumulating open time. 您只需对事件做出反应,注意事件发生的时间并累积开放时间。 In pseudo code, it gives : 用伪代码,它给出:

  • App starts in open mode: cumul = 0, openBeg = now, state = open 应用以打开模式启动:cumul = 0,openBeg =现在,state =打开
  • app starts in minimized mode : cumul = 0, state = min 应用程序以最小化模式启动:累积= 0,状态=最小值
  • app is minimized : cumul += now - openBeg, state = min 应用已最小化:累计+ =现在-openBeg,状态=分钟
  • app is (re-)opened : openBeg = now, state = open 应用已(重新)打开:openBeg =现在,状态=打开
  • app is closed : if state is open: cumul += now - openBeg 应用已关闭:如果状态为打开:累计+ =现在-openBeg

For python, now is time.time() and state should be managed as a boolean opened = True when state is open, opened = False when stat is min. 对于python,现在是time.time() ,状态应作为布尔值管理(当状态打开时为open opened = True ,当stat为min时open opened = False

It is simple if the application calls a similar API, it will be much more tricky if you have to hook on the windows application state changes. 如果应用程序调用类似的API,这很简单,但是如果您必须了解Windows应用程序状态的变化,它将变得更加棘手。

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

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