2016-12-17 62 views
2

我正在尝试做一个自动鼠标点击器。我现在的主要问题是如何为每个功能做一个计时器,例如,功能1工作大约15分钟,然后在15分钟开始工作一次后功能2,然后回到功能1.并且我想要功能4工程Independente酒店从其他人,我希望它点击即使函数1运行每次(林不知道这是可能的),这里是我的代码:Python函数的计时器

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) 

谢谢大家:)

+0

具有u试图导入时间,然后使用time.sleep(x)的x是几秒钟的延迟的数量。 –

+0

这意味着我应该让它没有功能,对吧? –

+0

但是,如果我这样做,那么函数1运行代码,然后睡15(不做任何事),然后function2开始,这不是重点。我想function1作为循环运行15分钟,然后运行一次函数2并返回到function1作为循环... –

回答

0

因为在不引入信号和多线程的情况下独立等待来管理多个函数并不容易,下面是用PyAutoGUI库管理多个函数的另一种方法。

的解决方案是一个多定序器类(称为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 

主回路功能,探索所有音序器,直到完成或 max_delay

# 
# 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 - 根据您声明的功能声明您的音序器

对于功能1(),使用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) 
+0

非常感谢你的答案,一点点更复杂,然后我有想象。我会尽力。再一次感谢你。 –