Home > Blockchain >  How do you get "Box" values out of "Array" in Python for use with `click()`?
How do you get "Box" values out of "Array" in Python for use with `click()`?

Time:11-18

NOTE: Using PyAutoGui library

I am trying to make python click on each icon on screen in order, I have gotten it to successfully print each item on screen with it's box values of left and top in place of X and Y coordinates. But I can't figure out how to get left/top converted into X/Y values for use with the pyautogui.click()

Code:

import pyautogui

coordinates = pyautogui.locateAllOnScreen('eachIcon.png')
for element in coordinates:
    print(element)

Prints:

Box(left=124, top=699, width=14, height=14)

What command would I use to extract Left and Top as X and Y number coordinates?

I am entirely new to python and practically new to coding (took a beginners class of C in college). I've spent a good hour googling and trying every term I can think of, I'm stumped: hence the post.

CodePudding user response:

coordinates.left coordinates.right etc should do the trick

CodePudding user response:

To do this you will need to unpack a tuple:

Box has 4 objects Box(left=124, top=699, width=14, height=14) to unpack Box we can just add 4 dummy variables (since we only need x,y) to your for code

for x, y, _, _ in coordinates: or for x, y, w, h in coordinates if you also want the weight and height

I inserted the dummy variables because otherwise we will get a ValueError. The error occurs when the number of variables doesn't match the number of values so if we had for x, y in coordinates: we would get a ValueError.

  • Related