You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 locateOnScreen to check if a target exists, then immediately calling locateCenterOnScreen again. Between these two quick calls, the game’s screen could shift (the pink/gold element might disappear), making the second function return None. When you try to unpack None into px, py or gx, 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 locateOnScreen and 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 a None value again.

This should eliminate that frustrating error while making your bot run more efficiently.

内容的提问来源于stack exchange,提问作者S_Hunt42

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:35:59