簡體   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)

任何人都知道為什么以及我應該怎么做才能防止此 TypeError: cannot unpack non-iterable NoneType object 解決方案

當您第二次調用locateOnScreen時,它會返回一個 none 對象,因為沒有找到您的模板圖像'archer.png'

這可能是由於在 while 循環的每次迭代中第一次調用以來屏幕狀態發生了變化。 如下所示只調用一次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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM