我使用pyautogui
编写了一个脚本,该脚本应该启动一个程序(IDE),然后开始使用它。
到目前为止,这是脚本:
#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui, sys, subprocess
from time import sleep
x,y = 1100,550
subprocess.call([r'C:\...exe', arg1, arg2])
pyautogui.click(x,y)
sleep(5) # 2 sec should suffice but this is for safety
pyautogui.typewrite(my_string)
pyautogui.press('enter')
这很好用,但我想携带方便。
x,y
值由启动程序后程序提示符在屏幕上出现的位置确定,但是我认为这不是可移植的。有没有一种方法可以将鼠标指向提示而不提供const参数?类似于move_mouse_to_window_of_this_process_after_starting_it()
另外,我使用
sleep()
以便在数据出现后将其写入窗口,但是我想这不是一个好方法(我猜某些PC的运行速度要慢得多),所以有一种方法可以知道何时出现提示,然后执行pyautogui.typewrite(my_string)
吗?编辑:我为
move_mouse_to_window_of_this_process_after_starting_it()
找到了一个简单的解决方案:
>>> pyautogui.hotkey('alt', 'tab')
最佳答案
如果您需要可移植且可靠的解决方案,则必须找到一个支持可访问性技术的库,以通过文本访问GUI元素。基本技术是:
Win32 API,MS UI自动化(Windows)
AT-SPI(Linux)
苹果辅助功能API(MacOS)
有多个开源GUI自动化库支持其中的某些技术(通常为1或2)。 Python解决方案:
Windows上的pywinauto(Win32 API和MS UIA均见,Getting Started Guide)
在Linux上的pyatspi2
在MacOS上为pyatom
还有关于硬睡眠与灵活等待的a thread on StackOverflow。
请享用! :)