PyAutoGUI 有一个内置的function called locateOnScreen()
如果可以在当前屏幕上找到图像,则返回图像中心的 x、y 坐标(它会截取屏幕截图,然后对其进行分析)。
图像必须匹配exactly为了这个工作;即如果您想单击button.png
该按钮图片必须与窗口中的按钮具有相同的精确尺寸/分辨率,以便程序识别它。实现此目的的一种方法是截取屏幕截图,在绘图中将其打开,然后仅剪切出您想要按下的按钮(或者您可以让 PyAutoGUI 为您完成此操作,正如我将在后面的示例中展示的那样)。
import pyautogui
question_list = ['greencircle', 'redcircle', 'bluesquare', 'redtriangle']
user_input = input('Where should I click? ')
while user_input not in question_list:
print('Incorrect input, available options: greencircle, redcircle, bluesquare, redtriangle')
user_input = input('Where should I click?')
location = pyautogui.locateOnScreen(user_input + '.png')
pyautogui.click(location)
上面的例子要求你已经有greencircle.png
以及目录中的所有其他 .png
PyAutoGUI 还可以截图并且您可以指定在屏幕的哪个区域进行拍摄pyautogui.screenshot(region=(0, 0, 0, 0))
前两个值是要选择的区域左上角的 x,y 坐标,第三个值是向右的距离 (x),第四个值是向下的距离 (y)。
以下示例截取 Windows 10 徽标的屏幕截图,将其保存到文件中,然后使用指定的 .png 文件单击该徽标
import pyautogui
pyautogui.screenshot('win10_logo.png', region=(0, 1041, 50, 39))
location = pyautogui.locateOnScreen('win10_logo.png')
pyautogui.click(location)
您也不必将屏幕截图保存到文件中,只需将其保存为变量即可
import pyautogui
win10 = pyautogui.screenshot(region=(0, 1041, 50, 39))
location = pyautogui.locateOnScreen(win10)
pyautogui.click(location)
让程序检测用户是否单击了某个区域(比方说,Windows 10 徽标)将需要另一个库,例如pynput.
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
if 0 < x < 50 and 1080 > y > 1041 and str(button) == 'Button.left' and pressed:
print('You clicked on Windows 10 Logo')
return False # get rid of return statement if you want a continuous loop
with Listener(on_click=on_click) as listener:
listener.join()
将所有内容放在一起
import pyautogui
from pynput.mouse import Listener
win10 = pyautogui.screenshot(region=(0, 1041, 50, 39))
location = pyautogui.locateOnScreen(win10)
# location[0] is the top left x coord
# location[1] is the top left y coord
# location[2] is the distance from left x coord to right x coord
# location[3] is the distance from top y coord to bottom y coord
x_boundary_left = location[0]
y_boundary_top = location[1]
x_boundary_right = location[0] + location[2]
y_boundary_bottom = location[1] + location[3]
def on_click(x, y, button, pressed):
if x_boundary_left < x < x_boundary_right and y_boundary_bottom > y > y_boundary_top and str(button) == 'Button.left' and pressed:
print('You clicked on Windows 10 Logo')
return False # get rid of return statement if you want a continuous loop
with Listener(on_click=on_click) as listener:
listener.join()