09-15-00
09-15-00

Reputation: 67

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)

Exact output:

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

It is working all fine if I put TradeFrom and TradeTo outside of while loop, can anyone explain why it breaks down after I put it inside while loop?

Upvotes: 2

Views: 5803

Answers (1)

Kuba
Kuba

Reputation: 195

I think you need to fix the logic. Right now you're looking for two objects on the screen. Your statement is saying that if the first object is not found but the second one is, then assign coordinates from the first object...which is None and that's why your code is failing. Not sure what the intended outcome is, but maybe try this:

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)

Also, I suggest you code in a break key so it's easy to interrupt this loop

Upvotes: 2

Related Questions