Python 如果语句无法执行,则使用 pyautogui 进行自动化

Python automation with pyautogui if statements wont execute

#! python3
import pyautogui
import time 
pyautogui.screenshot('first.png') #take a SS of my entire screen
check = pyautogui.locateCenterOnScreen('first.png') # set the value of check to center of screen assuming it will be the same as the SS...it will
print(check) #(960, 540) is the center of my screen and is stored in check
while True: #inf loop
    pyautogui.screenshot('Test.png') #take a second Screenshot
    idleout = pyautogui.locateCenterOnScreen('Test.png') #set the value of idleout with the center of test.jpg
    print(idleout) #(960, 540) is the center suprised??
if idleout != check: #idleout != (960, 540) then this should happen
    pyautogui.press('space') #space is never pressed or any action taken ive even tried with print("hello")

我想要发生的是,如果屏幕截图与我屏幕上当前显示的内容不匹配,它会执行一些操作。我运行在这里冒烟

我不确定 SO 上的格式是否设置正确(您的评论显示为关键字),但它看起来就像您的 if 语句之外while True 循环。如果是这样的话,它永远不会被执行(或检查)。

#! python3
import pyautogui
import time 
pyautogui.screenshot('first.png')
check = pyautogui.locateCenterOnScreen('first.png')
print(check)
while True:
    pyautogui.screenshot('Test.png')
    idleout = pyautogui.locateCenterOnScreen('Test.png')
    print(idleout)
    if idleout != check:
        pyautogui.press('space')