Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write a letter that matches the first letter of the image that is found on the screen

I have a list of images:

    def justAnObserver():
        kk = 0
        listOfLetterImages = ['A.png','B.png','C.png','D.png','E.png','F.png','G.png','H.png','I.png','J.png','K.png','L.png','M.png','N.    png','O.png','P.png','Q.png','R.png','S.png','T.png','U.png','V.png','W.png','X.png','Y.png','Z.png']

        while True:
            while True:
                for x in listOfLetterImages:
                    try:
                        if pyautogui.locateOnScreen(x, region=(350,337,98,103)) is not None:
                        print("jest")
                        kk = 1
                        break
                except Exception:
                    continue
            if kk == 1:
                kk=0
                break
            else:
                continue

And I want to, for example, press A on keyboard if A was found, or type R if R was found.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Simply search pyautogui press key will get what you want. See Keyboard Control Functions

x is the filename, x[0] gets the first char of the filename (for example, A), and .lower() convert it to lowercase for press function.

    def justAnObserver():
        kk = 0
        listOfLetterImages = ['A.png','B.png','C.png','D.png','E.png','F.png','G.png','H.png','I.png','J.png','K.png','L.png','M.png','N.    png','O.png','P.png','Q.png','R.png','S.png','T.png','U.png','V.png','W.png','X.png','Y.png','Z.png']

        while True:
            while True:
                for x in listOfLetterImages:
                    try:
                        if pyautogui.locateOnScreen(x, region=(350,337,98,103)) is not None:
                            pyautogui.press(x[0].lower())  # Here!
                            kk = 1
                            break
                except Exception:
                    continue
            if kk == 1:
                kk=0
                break
            else:
                continue

In addition, your code can be rewritten to this for readablility:

import string


def justAnObserver():
    listOfLetterImages = [i+".png" for i in string.ascii_uppercase]

    while True:
        for x in listOfLetterImages:
            try:
                if pyautogui.locateOnScreen(x, region=(350,337,98,103)) is not None:
                    pyautogui.press(x[0].lower())  # Here!
                    return
            except:
                pass
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading