分离 pyautogui.locateAllOnScreen() 返回的 x 和 y 值

Separating the x and y values returned by pyautogui.locateAllOnScreen()

目前我正在使用 python 并打开 cv 以 return 所需图像的 x 和 y 值。

我的代码中与问题相关的部分如下所示:

pathDesired = os.path.join('C:\Users', 'matlac', 'Desktop', 'png', 'desired.png')

desiredLocation = pyautogui.locateCenterOnScreen(desiredLocation, confidence=.6)
print(desiredLocation)

目前,当我打印时,它 return 是这样的

(898, 423)

有没有办法创建一个新变量,并将其设置为与我的 desiredLocation 相对应的数字 y 值?

它是 return 具有 x,y 坐标的元组。您已经将元组保存到 desiredLocation 变量,所以现在您只需要访问该项目。元组可以像列表一样被索引:

desiredLocation = pyautogui.locateCenterOnScreen(desiredLocation, confidence=.6)
x = desiredLocation[0]
y = desiredLocation[1]

Python 在从容器中的项目分配变量时也提供了一些非常优雅的语法,您可以将 return 值扩展为同一行上的两个变量:

x, y = pyautogui.locateCenterOnScreen(desiredLocation, confidence=.6)