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)
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()
PyAutoGUI有一个名为
locateOnScreen()
的内置function,如果它能在当前屏幕上找到图像中心,它将返回图像中心的x,y坐标(它拍摄一个屏幕截图,然后对其进行分析)图像必须与精确匹配才能工作;i、 e.如果你想点击一个
button.png
,那么按钮图片的大小/分辨率必须与窗口中的按钮相同,程序才能识别它。实现这一点的一种方法是拍摄一个屏幕截图,在画图中打开它,然后只剪下你想要按下的按钮(或者你可以让PyAutoGUI为你做这件事,我将在后面的示例中展示)上面的示例要求您的目录中已经有
greencircle.png
和所有其他.png文件PyAutoGUI还可以拍摄screenshots,您可以指定屏幕的哪个区域拍摄
pyautogui.screenshot(region=(0, 0, 0, 0))
前两个值是要选择的区域左上角的x,y坐标,第三个值是向右多远(x),第四个值是向下多远(y)以下示例获取Windows 10徽标的屏幕截图,将其保存到文件中,然后使用指定的.png文件单击徽标
您也不必将屏幕截图保存到文件中,只需将其保存为变量即可
让一个程序检测用户是否点击了某个区域(比如说windows 10徽标),需要另一个类似pynput的库
将所有内容放在一起
相关问题 更多 >
编程相关推荐