You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何让moveto类在定时click任务执行前后持续循环运行

问题修复方案

核心问题排查

  • 你定义的moveto是类而非可调用的函数/方法,类内部的代码会在Python加载类定义时执行仅一次,后续不会自动触发
  • 原主循环仅做了定时任务的待执行检测,没有加入鼠标移动的逻辑,所以定时任务等待期不会重复执行移动操作

修复后完整代码

import pyautogui
import time
import schedule

def moveto():
    time.sleep(5)
    pyautogui.moveTo(339, 261)
    time.sleep(3)

    pyautogui.moveTo(804, 255)
    time.sleep(3)

    pyautogui.moveTo(129, 751)
    time.sleep(3)

    pyautogui.moveTo(632, 754)
    time.sleep(3)


def click():
    # stops #
    time.sleep(3)
    pyautogui.press('`')

    # Opens #
    pyautogui.click(711, 1057)
    time.sleep(3)

    # clicks on #
    pyautogui.click(161, 266)
    time.sleep(3)

    # Writes #
    pyautogui.click(447, 993)
    pyautogui.typewrite('#')
    time.sleep(3)

    # Sends #
    pyautogui.press('enter')
    time.sleep(3)

    # clicks #
    pyautogui.click(1850, 11)
    print('#')
    time.sleep(3)

schedule.every().day.at("01:00").do(click)

while True:
    # 优先检查是否有待执行的定时任务
    schedule.run_pending()
    # 无待执行任务时运行鼠标移动逻辑
    moveto()

逻辑说明

  • 把moveto改造为普通函数,每次调用都会完整执行4次鼠标移动操作
  • 主循环优先检测定时任务是否到执行时间:如果到点就先执行click函数,执行完成后再回到鼠标移动的循环逻辑,满足你「定时任务运行前后都循环执行moveto」的需求
  • 所有原有功能逻辑完全保留,没有修改click内部的操作步骤

可选优化(按需选择)

如果需要让定时任务触发更及时(不用等完整一轮moveto跑完才执行),可以把moveto函数改造为每次等待后都检测定时任务:

def moveto():
    time.sleep(5)
    schedule.run_pending()
    pyautogui.moveTo(339, 261)
    time.sleep(3)
    schedule.run_pending()

    pyautogui.moveTo(804, 255)
    time.sleep(3)
    schedule.run_pending()

    pyautogui.moveTo(129, 751)
    time.sleep(3)
    schedule.run_pending()

    pyautogui.moveTo(632, 754)
    time.sleep(3)
    schedule.run_pending()

内容的提问来源于stack exchange,提问作者D3AD_SHOT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 17:45:04