使用PyAutoGui locateCenter函数进行Try/Except循环



由于双倍增量,该代码应该通过for循环的6次迭代/从技术上讲是3次迭代来执行。相反,我收到了一个错误。

如果需要代码的其余部分,请发表意见。

对我的代码的任何批评也很感激,任何建议都会很有帮助。

我的代码:

import pyautogui
import time
import random
import keyboard
import sys

def findClickShaftFunction():
count = 1
v = 1.0
movementTime = random.uniform(0.24, 1.88)
timeToSleep = random.uniform(25.32, 62.5)    
for count in range (0,5):
try:
print("Try:", count, " Sample:", count, " Image:", count)
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count), confidence=v)            
except TypeError:
continue
print("Try:", count+1, " Sample:", count+1, " Image:", count+1)
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count+1), confidence=v - 0.10)                      
#else:
#count = count + 1  
#print("Unable to trace " + count)

time.sleep(timeToSleep)
pictureOfShaftsX = pictureOfShaftsX - (random.randint(2,11))
pictureOfShaftsY = pictureOfShaftsY - (random.randint(2,11))
pyautogui.moveTo(pictureOfShaftsX, pictureOfShaftsY, movementTime)
pyautogui.click()

完整错误追溯:

Would you like to begin? Enter y to continue. y
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Traceback (most recent call last):
File "C:UsersPyAutoGui_Shaft_TrialPyAutoGui_Shaft_TrialPyAutoGui_Shaft_Trial.py", line 17, in findClickShaftFunction
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count), confidence=v)
TypeError: cannot unpack non-iterable NoneType object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersPyAutoGui_Shaft_TrialPyAutoGui_Shaft_TrialPyAutoGui_Shaft_Trial.py", line 78, in <module>
main()
File "C:UsersPyAutoGui_Shaft_TrialPyAutoGui_Shaft_TrialPyAutoGui_Shaft_Trial.py", line 56, in main
findClickShaftFunction()
File "C:UsersPyAutoGui_Shaft_TrialPyAutoGui_Shaft_TrialPyAutoGui_Shaft_Trial.py", line 20, in findClickShaftFunction
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count+1), confidence=v - 0.10)
TypeError: cannot unpack non-iterable NoneType object
Press any key to continue . . .

更换后

except TypeError:
print("Try:", count+1, " Sample:", count+1, " Image:", count+1)
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count+1), confidence=v - 0.10)   

except TypeError:
continue

在实际定位图像之前,它会遍历整个循环。

Would you like to begin? Enter y to continue. y
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5
Try: 0  Sample: 0  Image: 0
Try: 1  Sample: 1  Image: 1
Try: 2  Sample: 2  Image: 2
Try: 3  Sample: 3  Image: 3
Try: 4  Sample: 4  Image: 4
Try: 5  Sample: 5  Image: 5

我打算让循环做什么的例子(没有打印语句(

try:
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts0.png', confidence=0.7)      
except TypeError:
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts1.png', confidence=0.6)
try:
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts2.png', confidence=0.55)      
except TyperError:
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts3.png', confidence=0.50)      

据我所知,pyautogui.locateCenterOnScreen正在返回None,这就是您获得TypeError的原因。与其在except块中调用相同的方法,您可能只想将continue调用到循环的下一次迭代,如下所示:

for count in range(6):
try:
print("Try:", count, " Sample:", count, " Image:", count)
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count), confidence=v)
except TypeError:
continue

此外,请注意,range(1,6)从1-5(包括1-5(开始—您可能希望range(6)从0-5(包括0-5(开始(总共6次迭代(。

我认为这是我问题的合理答案。它似乎按预期进行迭代——检查每个图像。现在,我意识到TensorFlow可能对我的应用程序更好。

如有进一步建议,不胜感激。

count = 0;
v = 0.55

movementTime = random.uniform(0.24, 1.88)
timeToSleep = random.uniform(25.32, 62.5)    
success = False
while not success:
try:
print("Try:", count, " Sample:", count, " Image:", count)
pictureOfShaftsX, pictureOfShaftsY = pyautogui.locateCenterOnScreen('C:/Users/PyAutoGui_ImageRecognition/shafts{}.png'.format(count), confidence=v)
success = True
except TypeError:
count = count + 1           
pass

最新更新