Python之PyAutoGUI的使用
PyAutoGUI是Python的一个库,使用示例如下。
一、移动到某个坐标点进行点击
import pyautogui print(pyautogui.position()) pyautogui.moveTo(56, 18, duration=1) pyautogui.click()
二、依照图片相似度移动到某个图片的位置进行点击
import pyautogui pos = pyautogui.locateOnScreen("pic.png") center_pos = pyautogui.center(pos) pyautogui.moveTo(center_pos,duration=1) pyautogui.click()
三、点击记事本输入文本
import pyautogui print(pyautogui.position()) pyautogui.click(pyautogui.position(1161,1060)) pyautogui.typewrite("123456789",0.25) pyautogui.hotkey("ctrl","a") pyautogui.hotkey("ctrl","c") pyautogui.hotkey("ctrl","v") pyautogui.typewrite("\n") pyautogui.hotkey("ctrl","v")
四、对光标进行相对移动
import pyautogui pyautogui.moveTo(100,100) pyautogui.moveRel(100,50) print(pyautogui.position())
五、获取屏幕大小、判断坐标点是否在屏幕上
import pyautogui print(pyautogui.size()) print(pyautogui.onScreen(1921,1))
六、将光标移动到屏幕中央
import pyautogui size = pyautogui.size() print(pyautogui.moveTo(size.width / 2,size.height / 2))
编程中的报错及解决方法:
报错AttributeError: partially initialized module 'pyautogui' has no attribute 'position' (most likely due to a circular import)
是由于文件和模块重名了,换个文件名就好了。