简体   繁体   English

试图找到一种颜色并在 python 中单击它

[英]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.我是 python 的初学者,我对自动化感兴趣(键盘和鼠标移动。)为了尝试使用 pyautogui 的新东西,我想看看我是否可以看到图像或颜色,然后单击它。 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) button = "Playagain.png" pyautogui.doubleClick(button)

I tried to use a variable and the double click function in pyautogui but that did not work.我尝试使用变量并在 pyautogui 中双击 function 但这不起作用。 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它可能会在 10 秒或 30 秒后出现,这就是我想使用图像或颜色识别的原因,因为它的效率会提高 100 倍

Your example did not work because pyautogui.doubleclick takes screen coordinates as inputs.您的示例不起作用,因为pyautogui.doubleclick将屏幕坐标作为输入。 You need to use locate functions to locate an image on screen and then pass the coordinates of the detected image to doubleclick .您需要使用定位功能在屏幕上定位图像,然后将检测到的图像的坐标传递给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()

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

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