简体   繁体   English

Pyautogui:具有图像识别功能的自动点击器如何制作仅在识别图像时才运行的 function?

[英]Pyautogui : Autoclicker with Image recognition How do I make a function I made only run if an image is recognised?

I'm new in python and I know my code is really messy.我是 python 的新手,我知道我的代码真的很乱。 I need help, I wanted the function below to only run If an Image I set is recognised mc_ok_button.png but it's kind of confusing because I want it to join with a loop I already made.我需要帮助,我希望下面的 function 仅在我设置的图像被识别时运行mc_ok_button.png但这有点令人困惑,因为我希望它加入我已经制作的循环。 If I place the code in the loop wouldn't it not run the rest of the functions below it?如果我将代码放在循环中,它不会运行它下面的函数的 rest 吗?

My image recognition function:我的图像识别function:

import pyautogui as pg
import pydirectinput as pd
import time

ax,ay = pg.locateCenterOnScreen('mc_ok_button.png',confidence=.6)

def function_when_restart():
    print(ax,ay)
    pd.click(ax,ay)
    print('Waiting to return back to the server...')
    time.sleep(8)
    pd.moveTo(892,531)
    print('finding server...')
    pd.click()
    print('connecting to the server...')

function_when_restart()

Then I want the function_when_restart() function to only run if the mc_ok_button.png image is recognised and make project() and fix_tool() stop while function_when_restart() is running then continue back to make project() and fix_tool() :然后我希望function_when_restart() function 仅在mc_ok_button.png图像被识别并在function_when_restart()运行时停止project()fix_tool()时运行,然后继续返回make project()fix_tool()

from time import monotonic
from time import monotonic, sleep

start = monotonic()
        while monotonic() - start < (60) : #Run for exactly 1 minute
            # I want the image recognition to always run and if the image pops up I want 
            project()
            sleep(5)
            fix_tool()

below can help?下面可以帮忙吗?

while True:
     image = pyautogui.locateOnScreen('image.png')
     if image != None:
         project()
         sleep(5)
         fix_tool()
     else:
        pass

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

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