在元组中解包返回值的问题

problems with unpacking returned values in tuples

我有一个获取文件参数的函数,该函数在屏幕上查找该文件的坐标,然后 returns 它们。 我知道这个问题可能很愚蠢,但请帮忙。

redx,redy = somefunc(Ui_Ellements[9], 12)

def somefunc(file,index):
    .....
    x=int((pt[0]*2+w)/2)
    y=int((pt[1]*2+h)/2)
    print("Found "+file,x,y)
    if index==12:
        return (x,y)

一切正常,但我遇到了这样的问题

redx,redy = somefunc(Ui_Ellements[9], 12) TypeError: cannot unpack non-iterable int object

我试过了:
1.

def move(index):
    print(index)
    for i in index:
        print(i)
red = somefunc(Ui_Ellements[9], 12)
move(red)

def somefunc(file,index):
    .....
    x=int((pt[0]*2+w)/2)
    y=int((pt[1]*2+h)/2)
    print("Found "+file,x,y)
    if index==12:
        return (x,y)
red = somefunc(Ui_Ellements[9], 12)
print(red[0])
red = somefunc(Ui_Ellements[9], 12)
print(red[-1])

P.s 所有数据都是正确的,如果我尝试

print(red)

输出数据

(1205, 306)


def somefunc(file,index):
global sens
global top
global left
global Resolution
time.sleep(speed)
if index ==7 or index ==12 and file != chekers[8] :
    img = cv2.imread('files/'+Resolution+'/part.png')
else:
    screen()
    img = cv2.imread('files/'+Resolution+'/screen.png')  # картинка, на которой ищем объект
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # преобразуем её в серуюш
template = cv2.imread('files/'+Resolution+'/'+file,cv2.IMREAD_GRAYSCALE)  # объект, который преобразуем в серый, и ищем его на gray_img
w, h = template.shape[::-1]  # инвертируем из (y,x) в (x,y)`
result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= sens)
if index =='battletag':
    print(loc)
    if len(loc[0]) != 0:
        j=0
        for pt in zip(*loc[::-1]):
            Flag=False
            for num in range(3):
                if pt[0] < enemywiz[num] + 10 and pt[0] > enemywiz[num] - 10:
                    Flag=True
                    pass
            if not Flag:
                for num in range(3):
                    if enemywiz[num] == 0 :
                        enemywiz[num]=pt[0]
                        x = (pt[0] * 2 + w) / 2
                        y = (pt[1] * 2 + h) / 2
                        enemywizF[j]=x,y
                        j+=1
                        break
        for i in range(2):
            enemywiz[i]=0
        return 0
if len(loc[0]) !=0:
    for pt in zip(*loc[::-1]):
        pt[0] + w
        pt[1] + h
    x=int((pt[0]*2+w)/2)
    y=int((pt[1]*2+h)/2)
    print("Found "+file,x,y)
    if index==12:
        return (x,y)
    if (index==6 or file == Ui_Ellements[5] or file == chekers[7]):
        global xm
        global ym
        xm=x
        ym=y
        return True
    if file == chekers[8]:
        if index ==7:
            xm+=left
            ym+=top
        ahk.mouse_move(xm, ym, speed=2)
        time.sleep(0.5)
        ahk.mouse_drag(x, y, relative=False)
        return True
    if file == chekers[5]:
        ahk.mouse_move(x, y+70, speed=3)  # Moves the mouse instantly to absolute screen position
        ahk.click()
        return True
    if file ==buttons[5]:
        ahk.mouse_move(x, y, speed=5)
        return True
    if index == 1:
        return True
    if index == 7:
        xm = x
        ym = y
        return True
    ahk.mouse_move(x, y, speed=5)  # Moves the mouse instantly to absolute screen position
    ahk.click()  # Click the primary mouse button
    if file ==buttons[7]:
        return True
    if file == Ui_Ellements[3]:
        group_create()
else:
    print("Not found  "+file)
    if index == 12:
        return 0
  • 在大多数常见情况下,您需要保持函数具有基本相同类型的输出。
  1. 您可以将 return 0 替换为 return 0,0
  2. 或者您可以将其替换为raise Exception(f"Not found {file}")
  • 我不知道你到底想要什么return当你return True时,你也可以用相同的样式替换它,例如raise Exception(f"can't get redx and redy")

示例:

def test(x,y):
    index = x + y
    if index == 12:
        return (x,y)
    else:
        raise Exception("Not found file")
        # return 0,0
try:
    a,b = test(2,3)
    print(a,b)
except Exception as e:
    print(e)

try:
    a,b = test(7,5)
    print(a,b)
except Exception as e:
    print(e)

结果:

Not found file
7 5