【问题标题】:Timer for python functionspython函数的计时器
【发布时间】:2016-12-17 13:21:53
【问题描述】:

我正在尝试做一个自动鼠标点击器。我现在的主要问题是如何为每个功能做一个计时器,例如,功能 1 工作大约 15 分钟,然后功能 2 在 15 分钟后开始工作一次,然后返回功能 1。我想要功能 4独立于其他人,我希望它每次都点击,即使功能 1 正在运行(我不确定这是否可能)这是我的代码:

    import pyautogui, sys
pyautogui.size()
(1280, 800)

def function1():
        pyautogui.click(button='left', x=619, y=266) 
        pyautogui.PAUSE = 3.9
        pyautogui.click(button='left', x=617, y=475)

def function2():
        pyautogui.click(button='left', x=624, y=347)
        pyautogui.PAUSE = 5.0
        pyautogui.click(button='left', x=615, y=431)
        pyautogui.PAUSE = 5.0
        pyautogui.click(button='left', x=315, y=483)
        pyautogui.PAUSE = 5.0
        pyautogui.click(button='left', x=616, y=390)


def function3 ():
        pyautogui.click(button='left', x=617, y=522)
        pyautogui.PAUSE = 5.0


def function4():
        pyautogui.click(button='left', x=1257, y=432)

谢谢大家:)

【问题讨论】:

  • 你是否尝试过导入时间,然后使用 time.sleep(x) x 作为延迟秒数。
  • 这意味着我应该让它没有功能,对吧?
  • 但是,如果我这样做了,那么函数 1 运行代码,然后休眠 15(什么都不做),然后函数 2 启动,这不是重点。我想 function1 作为循环运行 15 分钟,然后运行一次 function 2 并作为循环返回到 function1...
  • 你能添加调用所有函数(1到4)的源代码的主要部分吗?我不认为你可以独立于其他运行函数调用function4()
  • splash()storm() 防御()pick()

标签: python function timer mouse


【解决方案1】:

由于在不引入信号和多线程的情况下,单独使用wait来管理多个函数并不容易,这里提供了另一种使用PyAutoGUI库管理多个click()函数的方法。

解决方案是一个多序列器类(称为class TimerExec)。

第 1 步 - 使用 class TimerSeq 存储音序器参数

只需要构造函数

class TimerSeq:
    def __init__(self, iseq, tseq, bexe, bloop):    
        self.iseq = iseq
        self.tseq = tseq
        self.bexe = bexe
        self.bloop = bloop

第 2 步 - 创建一个 class TimerExec 来管理排序器列表

构造函数创建一个空列表

class TimerExec:
    def __init__(self):
        self.list = [ ]
        self.stop = True
        self.chrono = -1
        self.delay = -1

一个简单的浮点秒值到 int 毫秒

#
# convert float seconds to milliseconds
def tomilli(self, fsec):
    return int(round(fsec * 1000))

在列表中添加一个新的排序器

#
# append new sequences to the list
def append(self, func, loop=False):
    self.list.append([func, TimerSeq(-1, -1, False, loop)])
    print('list:',self.list)

验证序列器是否完成或循环时重新启动

#
# check end of sequence or restart
def nextcheck(self, seq):
    if seq[1].iseq >= len(seq[0]):
        if seq[1].bloop:
            seq[1].iseq = 0 # restart
        return True
    return False

计算当前序列器中下一个序列的参数

#
# switch to the next sequence
def nextstep(self, seq):
    if seq[1].iseq >= len(seq[0]):
        return True
    seq[1].iseq = seq[1].iseq+1
    seq[1].tseq = self.tomilli(time.time())
    seq[1].bexe = False
    return False

管理当前的序列器然后执行当前的函数 延迟下一个序列

#
# explore sequence and execute when  
def exestep(self, seq):
    bseq = False
    if seq[1].tseq < 0:
        bseq = self.nextstep(seq)
    else:
        bseq = self.nextcheck(seq)
    if bseq:
        return True
    pseq = seq[0][seq[1].iseq]
    tnow = self.tomilli(time.time())
    tdel = self.tomilli(pseq[0])
    if seq[1].bexe == False:
        print('execute(%d):'% (tnow-self.chrono),pseq)
        # execute the selected function
        pseq[1](pseq[2],pseq[3],pseq[4])
        seq[1].bexe = True 
    tseq = seq[1].tseq
    if tnow > (tseq+tdel):
        bseq = self.nextstep(seq)
    return bseq

探索所有音序器直到完成的主循环函数或 最大延迟

#
# loop to execute all sequences with max_delay (s)
def execute(self, max_delay):
    print('start:',time.strftime("%H:%M:%S", time.localtime()))
    self.stop = False
    self.delay = self.tomilli(max_delay)
    self.chrono = self.tomilli(time.time())
    while self.stop == False:
        tnow = self.tomilli(time.time())
        #if tnow > (self.chrono + self.delay):
        #    break
        bstop = True
        for seq in self.list:
            bseq = self.exestep(seq)
            bstop = bstop & bseq
        if bstop == True:
            self.stop = True    
    print('stop:',time.strftime("%H:%M:%S", time.localtime()),
          ((tnow-self.chrono)/1000.0))

第 3 步 - 根据您声明的函数声明您的音序器

对于 function1(),使用 2 步音序器:

def function1():
        pyautogui.click(button='left', x=619, y=266) 
        pyautogui.PAUSE = 3.9
        pyautogui.click(button='left', x=617, y=475)

排序器是:

fct1 = [
  # pyautogui.click(button='left', x=619, y=266) 
  [ 3.9, pyautogui.click, 'left', 619, 266 ],
  # pyautogui.click(button='left', x=617, y=475)
  [ 0.0, pyautogui.click, 'left', 617, 475 ]
]

第 4 步 - 创建 TimerExec 对象,添加序列器然后执行。

持续时间最长为 13.6 秒

tSeq = TimerExec()
tSeq.append(fct1)
tSeq.execute(13.6)

【讨论】:

  • 非常感谢您的回答,比我想象的要复杂一些。我会尽力。再次感谢您。
猜你喜欢
  • 2021-01-22
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2017-12-11
相关资源
最近更新 更多