Python点击器机器人报错TypeError:无法解包非可迭代NoneType对象
Hey there, let's break down why you're hitting that TypeError: cannot unpack non-iterable NoneType object error and fix it up!
The Root Cause
Your code has two key issues triggering this error:
- You’re calling
locateOnScreento check if a target exists, then immediately callinglocateCenterOnScreenagain. Between these two quick calls, the game’s screen could shift (the pink/gold element might disappear), making the second function returnNone. When you try to unpackNoneintopx, pyorgx, gy, that’s where the crash happens. - Even if the screen doesn’t change, scanning it twice is redundant and wastes unnecessary system resources.
The Fix
Instead of scanning the screen twice, capture the location once, verify it’s not None, then get its center directly. Here’s your adjusted code:
from pyautogui import * import pyautogui import time import keyboard import win32api, win32con def click(x,y): win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) # Initial eyeloc value (only keep if you use it elsewhere) eyeloc = (997, 456) while not keyboard.is_pressed('q'): # Check for pink target first pink_loc = pyautogui.locateOnScreen('pink.png', region=(576, 160, 842, 592), confidence=0.8) if pink_loc is not None: px, py = pyautogui.center(pink_loc) print("pink") pyautogui.moveTo(px, py + 130, 0.2) time.sleep(0.4) # Check for gold target if pink isn't found else: gold_loc = pyautogui.locateOnScreen('gold.png', region=(576, 160, 842, 593), confidence=0.8) if gold_loc is not None: gx, gy = pyautogui.center(gold_loc) print("gold") pyautogui.moveTo(gx, gy + 130, 0.2) time.sleep(0.4) # Optional: Add a tiny delay here to lower CPU usage # time.sleep(0.1)
What Changed?
- We capture the screen location once with
locateOnScreenand store it in a variable (pink_loc/gold_loc), avoiding redundant scans. - We use
pyautogui.center()to get the target’s center from the already-located region—this is faster and removes the risk of the target disappearing between two separate scans. - We only attempt to unpack coordinates after confirming the location isn’t
None, so we never try to unpack aNonevalue again.
This should eliminate that frustrating error while making your bot run more efficiently.
内容的提问来源于stack exchange,提问作者S_Hunt42
相关产品推荐
相关产品推荐

