PyAutoGui TypeError: cannot unpack non-iterable NoneType object

PyAutoGui TypeError: cannot unpack non-iterable NoneType object

    import pyautogui
import cv2
import time
while(True):
    TradeFrom = pyautogui.locateCenterOnScreen("Screenshot_256.png", grayscale= True, confidence=0.9)
    TradeTo = pyautogui.locateCenterOnScreen("Screenshot_258.png", grayscale=True, confidence=0.9)
    if TradeFrom == None:
        if TradeTo == None:
           time.sleep(30)
           continue
        elif TradeTo != None:
           z,t=TradeFrom
           pyautogui.moveTo(z,t,3)
           pyautogui.rightClick()
           #TODO ÇOK YAPILACAK ŞEY VAR AMK
    else:
        x,y = TradeTo
        pyautogui.moveTo(x,y,3)
        pyautogui.rightClick()
        InviteToParty = pyautogui.locateCenterOnScreen("InviteToParty.png", grayscale= True, confidence=0.9)
        Invite_X,Invite_Y = InviteToParty
        pyautogui.moveTo(Invite_X,Invite_Y)

精确输出:

Traceback (most recent call last):
  File "C:/Users/emosc/PycharmProjects/heuheu/main.py", line 12, in <module>
    z,t=TradeFrom
TypeError: cannot unpack non-iterable NoneType object

如果我将 TradeFrom 和 TradeTo 放在 while 循环之外,一切正常,谁能解释为什么我把它放在 while 循环中后它会崩溃?

我认为您需要修正逻辑。现在你正在寻找屏幕上的两个对象。您的陈述是说,如果找不到第一个对象但找到了第二个对象,则从第一个对象分配坐标...这是 None 这就是您的代码失败的原因。不确定预期的结果是什么,但也许试试这个:

import pyautogui
import cv2
import time
while(True):
    TradeFrom = pyautogui.locateCenterOnScreen("Screenshot_256.png", grayscale= True, confidence=0.9)
    TradeTo = pyautogui.locateCenterOnScreen("Screenshot_258.png", grayscale=True, confidence=0.9)
    if TradeFrom == None:
        if TradeTo == None:
           time.sleep(30)
           continue
        elif TradeTo != None:
           z,t=TradeTo
           pyautogui.moveTo(z,t,3)
           pyautogui.rightClick()
           #TODO ÇOK YAPILACAK ŞEY VAR AMK
    else:
        x,y = TradeFrom
        pyautogui.moveTo(x,y,3)
        pyautogui.rightClick()
        InviteToParty = pyautogui.locateCenterOnScreen("InviteToParty.png", grayscale= True, confidence=0.9)
        Invite_X,Invite_Y = InviteToParty
        pyautogui.moveTo(Invite_X,Invite_Y)

此外,我建议您编写一个 break 键,这样可以很容易地中断此循环