电脑上控制所有软件,比如说微信自动发消息,QQ,
1.先上功能,直接自动化发消息
我们首先需要定位窗口坐标,
import pyautogui as pg
print(pg.position())
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pyautogui as pg import pyperclip import time time.sleep(5) msg = "aaa" pg.click(x=90, y=896) pyperclip.copy(msg) pg.hotkey("ctrl","v") pg.typewrite("\n")
2.pyperclip基本api(粘贴复制)
import pyperclip # 复制 pyperclip.copy('Hello world!') # 粘贴 print(pyperclip.paste())
3.pyautogui基本api(自动,鼠标,键盘,屏幕事件)
3.1定位操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1、获取屏幕宽高 screenWidth, screenHeight = pyautogui.size() 屏幕左上开始为(0,0) 2、获取当前鼠标位置 currentMouseX, currentMouseY = pyautogui.position() onScreen(0, 0) ,检查是否在屏幕上 3、根据图片定位在图片在屏幕上的中点 x, y = pyautogui.locateCenterOnScreen(r'C:\screenshot.png') 4、 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出 pyautogui.locateAllOnScreen(r'C:\region_screenshot.png') for pos in pyautogui.locateAllOnScreen(r'C:\\region_screenshot.png'): print(pos)
3.2屏幕操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1、截取部分屏幕 im = pyautogui.screenshot(r'C:\screenshot.png',region=(0, 0, 300, 400)) # 截屏并设置保存图片的位置和名称 2、 匹配屏幕所有与目标图片的对象,可以用for循环和list()输出 pyautogui.locateAllOnScreen(r'C:\region_screenshot.png') for pos in pyautogui.locateAllOnScreen(r'C:\\region_screenshot.png'): print(pos) 3、获取屏幕点色彩属性值 pix = pyautogui.screenshot().getpixel((x, y)) #0,1,2=rgb 4、灰度值匹配 pyautogui.pixelMatchesColor(100, 200, (205, 255, 245), tolerance=10)
3.3鼠标操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1、鼠标移动 pyautogui.moveTo(100, 150) #绝对位置 pyautogui.moveRel(None, 10) #相对位置 pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad) # 2秒带动画移动到500,500 2、鼠标点击 pyautogui.click() 单击 pyautogui.click(x=100, y=200) # pyautogui.doubleClick() 双击 pyautogui.rightClick() 单击鼠标右键 pyautogui.middleClick() 单击鼠标中键 pyautogui.click(button='right', clicks=3, interval=0.25) 3、鼠标拖动 pyautogui.dragTo(x,y[,duration=t) pyautogui.dragRel(x,y, duration=0.5) 按住左键移动 4、鼠标滚动 pyautogui.scroll(10) / -10 # 正数向上/下滚动 pyautogui.hscroll(10) / -10 # 向右/左滚动
3.4鼠标移动例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import pyautogui,time screenWidth, screenHeight = pyautogui.size() pyautogui.moveTo(screenWidth / 2, screenHeight / 2) distance = 200 while distance > 0: pyautogui.dragRel(distance, 0, duration=0.5) # move right distance -= 5 pyautogui.dragRel(0, distance, duration=0.5) # move down pyautogui.dragRel(-distance, 0, duration=0.5) # move left distance -= 5 pyautogui.dragRel(0, -distance, duration=0.5) # move up
3.5键盘操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
pyautogui.typewrite('Hello world!', interval=0.25) #模拟输入 typewrite(['enter']) pyautogui.typewrite(['a','b','left','left','X','Y']) # XYab pyautogui.press('esc') pyautogui.keyDown('shift') pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left']) 连续按键left pyautogui.keyUp('shift') pyautogui.hotkey('ctrl', 'c') # 组合键
3.6键盘键说明
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
键盘键字符串 含义 'a','b','c','A','C','1','2','3', 单个字符的键 '!','@','#'等 'enter' 回车 ‘esc' ESC键 'shiftleft','shiftright' 左右Shift键 'altleft','altright' 左右Alt键 'ctrlleft','ctrlright' 左右Ctrl键 ‘tab'(or '\t') Tab键 'backspace','delete' Backspace键和Delete键 'pageup','pagedown' Page Up 和Page Down键 'home','end' Home键和End键 'up','down','left','right' 上下左右箭头键 'f1','f2','f3'等 F1至F12键 'volumemute','volumeup',volumedown' 静音,放大音量和减小音量键 'pause' 暂停键 'capslock','numlock','scrolllock' Caps Lock,Num Lock和 Scroll Lock键 'insert' Insert键 'printscreen' Prtsc或Print Screen键 'winleft','winright' 左右Win键(在windows上) 'command' Command键(在OS X上) 'option' Option键(在OS X上)
-----------------------------------------------------------------------------------------------------------------------------------------