简体   繁体   中英

Trying to find a colour and click on it in python

I am a beggineer in python and I am interested in automation, (keyboard and mouse movements.) And to try something new with pyautogui I wanted to see if I can see an image or colour and then click on it. To do this i thought an offline game might be good since i can do it in my own time and it would be a fun project.

button = "Playagain.png" pyautogui.doubleClick(button)

I tried to use a variable and the double click function in pyautogui but that did not work. and I tried to see if i could grab the colour of the button but that is diffucult and i didn't understand that. So if anyone could help me that would be very good!

Some extra bits of infomation might be. The Play again button is allways in the same place but it comes up at diffrent times. It might come up at 10 seconds or 30 seconds and that's why i wanted to use image or colour recognition because it would be 100x more efficient

Your example did not work because pyautogui.doubleclick takes screen coordinates as inputs. You need to use locate functions to locate an image on screen and then pass the coordinates of the detected image to doubleclick . This is an example from the docs:

import pyautogui
x, y = pyautogui.locateCenterOnScreen('image.png')
pyautogui.click(x, y)
import pyautogui

color = (75, 219, 106)

def clicker():
    while True:
        x, y = pyautogui.position()
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        if pixelColor == color:
            pyautogui.click()

def main():
    while True:
        clicker()

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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