[英]Python pyautogui bot works for some time and then TypeError: cannot unpack non-iterable NoneType object Solution
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
time.sleep(3)
def click(x, y):
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
while keyboard.is_pressed('q') == False:
if pyautogui.locateOnScreen('archer.png', confidence=0.8, region=(260, 760, 1400, 200)) is not None:
width, high, g, b = pyautogui.locateOnScreen('archer.png', confidence=0.8, region=(260, 760, 1400, 200))` `
click(width, high)
Any one knows why and what should i do to prevent this TypeError: cannot unpack non-iterable NoneType object Solution任何人都知道为什么以及我应该怎么做才能防止此 TypeError: cannot unpack non-iterable NoneType object 解决方案
When you call locateOnScreen
a second time, it is returning a none object because of not locating your template image 'archer.png'
.当您第二次调用
locateOnScreen
时,它会返回一个 none 对象,因为没有找到您的模板图像'archer.png'
。
This could be caused by the screen state changing since your first call in every iteration of the while loop.这可能是由于在 while 循环的每次迭代中第一次调用以来屏幕状态发生了变化。 Calling
locateOnScreen
only once as shown below should fix the error but since your screen state seems to be changing, whatever you want to click on the screen might disappear before the mouse moves to the identified location.如下所示只调用一次
locateOnScreen
应该可以修复错误,但由于您的屏幕状态似乎正在发生变化,因此您想在屏幕上单击的任何内容都可能在鼠标移动到指定位置之前消失。
while keyboard.is_pressed('q') == False:
imageBox = pyautogui.locateOnScreen('archer.png', confidence=0.8, region=(260, 760, 1400, 200))
# imageBox is none if archer.png is not located
# otherwise, it is a four-tuple representing the left coordinate, top coordinate,
# width and height of a box surrounding the location of archer.png
if imageBox is not None:
click(imageBox[0], imageBox[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.