Currently I have this
for word in words:
print(word)
but now i want to introduce a while loop, to only execute that as long as an image (spinner.png) is located on the screen. like this
while True:
try:
location = pyautogui.locateOnScreen('c:/images/spinner.png', region=(140, 240, 300, 100), confidence=0.5)
for word in words:
print(word)
except pyautogui.ImageNotFoundException:
pass
OR like this:
for word in words:
while True:
try:
location = pyautogui.locateOnScreen('c:/images/spinner.png', region=(140, 240, 300, 100), confidence=0.5)
print(word)
except pyautogui.ImageNotFoundException:
pass
and looking for the pythonic way also. but confused by reading many kb on break in the inner loop.
I have not tried anything as yet, just want to know the most efficient way to code this
while True:
try:
location = pyautogui.locateOnScreen('c:/images/spinner.png', region=(140, 240, 300, 100), confidence=0.5)
for word in words:
print(word)
except pyautogui.ImageNotFoundException:
pass
0
I think your first approach works best because you only need to check if the image appear on screen once and then print all elements in the possible_words
list. This way you will only need to check once whereas the second option you have to check it for every element in possible_words
list. Also I think it would be better to add a break
in the while
loop to escape the loop once the image appears on screen to avoid being stuck in an infinite loop
while True:
try:
location = pyautogui.locateOnScreen('c:/images/spinner.png', region=(140, 240, 300, 100), confidence=0.5)
for word in possible_words:
print(word)
break
except pyautogui.ImageNotFoundException:
pass
1