Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

python - How to make endless While loop for checking a state?

while pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)) != None:
    playsound('click.mp3')
    if pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)) == None:
        continue

pyautogui takes a screenshot and checks whether my image is in certain region or not, I want it to keep repeating

playsound('click.mp3')

when the image is visible, but when its not I want it to keep checking (repeating pyautogui.locateOnScreen) if the image is visible again . So far my code ends when I hide the image, I dont know how to keep it as "Standby".
Also, Im having a very hard time understanding the continue function, does it go back and read while again and again?

EDIT
I also tried this

while pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)) is not None:
    playsound('click.mp3')
else:
    continue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to have two different states:

while True:

    while pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)):
        playsound('click.mp3')
    
    while not pyautogui.locateOnScreen('white4x4.png', region = (200,200,4,4)):
        pass

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...