pyautogui GIMP自动化问题

pyautogui GIMP automation problems

我正在使用 GIMP 作为我的测试用例进行一些自动化操作。我需要启动程序并向其发送 mouse/keyboard 操作。我正在使用 subprocess.call() 启动程序并使用 pyautogui 发送 mouse/keyboard 操作。

import pyautogui
import subprocess

subprocess.call('gimp', shell=True)    
gimp_file = pyautogui.locateOnScreen('imgs/gimp_file.png', grayscale=True,region=(5, 28, 29, 19))

file_button_x, file_button_y = pyautogui.center(gimp_file)  
pyautogui.click(file_button_x, file_button_y, duration=0.75)

我遇到的问题是代码的 pyautogui 部分从未被命中,程序似乎只是挂起。我已经 运行 通过 PyCharm 中的调试器,但没有返回任何变量。如果我 运行 subprocess.call('gimp') 在 python 控制台中,其余代码在另一个控制台中,它会做它应该做的。我认为可能是程序启动导致的时间延迟或window焦点;我添加了 time.sleep(5) 来解决前一个原因,但没有影响。我该如何解决这个问题?

subprocess.call() 将等待进程退出——这不会发生(除非您退出 GIMP),您也不希望它等待。您应该使用 subprocess.Popen(),将 stdin 设置为 subprocess.DEVNULL,将 stdout/stderr 单独保留或重定向到 DEVNULL。这将允许您的程序继续执行 pyautogui 行,而 GIMP 是 运行.

注意 gimp 会显示启动画面,因此您可能需要稍等片刻才能尝试在其中找到它的 window 和 'click'。