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.
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