JunKim
JunKim

Reputation: 807

how to run infinite while loop until it finds the value in Python?

I am relatively new to Python and learning with fun so far.

What I am trying to do is finding the position of button using Python and its library, Pyautogui.

Here is my code.

import webbrowser, pyautogui, time, datetime

class workDoneClicker:

    def buttonTrack(self):
        global x, y
        x = ()
        y = ()
        while x == ():
            coordinate = pyautogui.locateOnScreen('image.png')
            x, y = (coordinate[0], coordinate[1])
            return x, y

    def clicker(self):         

        if pyautogui.alert(text="hi", title="hi") == 'OK':
            webbrowser.open('http://example.com')
            time.sleep(3)
            self.buttonTrack()
            self.clickButton()
            print("executed")

        else:
           print("not executed")

What I want to do is to execute the buttonTrack function until it finds the value, and return x, y.
And run the next code in clicker function.
Getting the value using buttonTrack function takes some seconds since it has to load a webpage.
But when I run the code clicker, it seems that it doesn't do infinite loop until it finds the value but runs the next code since I am getting 'NoneType' object is not subscriptable.

May I ask how to run as I expected? and explanation?

Upvotes: 1

Views: 1531

Answers (3)

Gaurav Dhameeja
Gaurav Dhameeja

Reputation: 362

I've never used this API before, but going through the docs and details in your question I shall try to answer. To run an infinite loop you can just use while True: In context of your question:

x = ()
y = ()
while True:
    coordinate = pyautogui.locateOnScreen('image.png')
        if coordinate:
            x, y = (coordinate[0], coordinate[1])
            return x, y

Upvotes: 0

Sneha
Sneha

Reputation: 140

The pyautogui.locateOnScreen() function is returning None when the button is not found and you are trying to do coordinate[0] which is throwing an error as None is not subscriptable. You can add a check that if the value of coordinate is not None then only populate x and y value.

class workDoneClicker:
  def buttonTrack(self):
    global x, y
    x = ()
    y = ()
    while x == ():
        coordinate = pyautogui.locateOnScreen('image.png')
        if(coordinate is not None):
            x, y = (coordinate[0], coordinate[1])
            return x, y
def clicker(self):
    if pyautogui.alert(text="hi", title="hi") == 'OK':
        webbrowser.open('http://example.com')
        time.sleep(3)
        self.buttonTrack()
        self.clickButton()
        print("executed")
    else:
        print("not executed")

Upvotes: 1

Seljuk Gulcan
Seljuk Gulcan

Reputation: 1868

If the image can’t be found on the screen, locateOnScreen() returns None.

http://pyautogui.readthedocs.io/en/latest/screenshot.html

So if image.png is not found, coordinate becomes None which throws error on the next line since you cannot do [0] on None object.

Add a None condition and it should be working.

coordinate = pyautogui.locateOnScreen('image.png')
if coordinate is not None:
    x, y = (coordinate[0], coordinate[1])
    return x, y

Upvotes: 0

Related Questions