Python pyautogui bot works for some time and then TypeError: cannot unpack non-iterable NoneType object Solution
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])
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])