简体   繁体   English

Python pyautogui bot 工作一段时间后出现TypeError: cannot unpack non-iterable NoneType object 解决方法

[英]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.

相关问题 PyAutoGui TypeError:无法解压不可迭代的NoneType object - PyAutoGui TypeError: cannot unpack non-iterable NoneType object Pyautogui Image 工作一段时间后不可迭代,TypeError: cannot unpack non-iterable NoneType object - Pyautogui Image non-iterable after working for a while, TypeError: cannot unpack non-iterable NoneType object Pyautogui - 单击图像:无法解压不可迭代的 NoneType 对象 - Pyautogui - click on image: cannot unpack non-iterable NoneType object "有人可以向我解释为什么 pyautogui TypeError: cannot unpack non-iterable NoneType object" - Can someone explain to me why pyautogui TypeError: cannot unpack non-iterable NoneType object -- TypeError: cannot unpack non-iterable NoneType object - -- TypeError: cannot unpack non-iterable NoneType object Numpy 抛出 TypeError: cannot unpack non-iterable NoneType object - Numpy throwing TypeError: cannot unpack non-iterable NoneType object 方法 Euler TypeError: cannot unpack non-iterable NoneType object - Method Euler TypeError: cannot unpack non-iterable NoneType object TypeError:无法解压不可迭代的 NoneType 对象 - TypeError: cannot unpack non-iterable NoneType object TypeError:无法解压不可迭代的 NoneType 对象错误 - TypeError: cannot unpack non-iterable NoneType object Error TypeError: cannot unpack non-iterable Message object in discord bot - TypeError: cannot unpack non-iterable Message object in discord bot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM