【问题标题】:Python pyautogui bot works for some time and then TypeError: cannot unpack non-iterable NoneType object SolutionPython pyautogui bot 工作一段时间然后TypeError: cannot unpack non-iterable NoneType object 解决方法
【发布时间】:2021-10-05 03:34:24
【问题描述】:
    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 解决方案

【问题讨论】:

  • 请注意,pyautogui 有一个 click function,您可以使用它来代替您的

标签: python bots pyautogui


【解决方案1】:

当您第二次调用locateOnScreen 时,由于未找到您的模板图像'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])

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2020-08-26
    • 2021-11-27
    • 1970-01-01
    • 2021-09-24
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多