获取基础信息
import pyautogui
#屏幕大小
size = pyautogui.size()
print(size)
Size(width=1920, height=1080)
#鼠标位置
mouse_pos = pyautogui.position()
print(mouse_pos)
Point(x=0, y=0)
#判断点是否在屏幕内
print(pyautogui.onScreen(100,100))
True
移动鼠标
size = pyautogui.size()
#把鼠标移动到(10,10)的位置,周期为1秒 (x,y,duration)
pyautogui.moveTo(10,10,duration=1)
#把鼠标移动到画面中央,周期为0.5秒
pyautogui.moveTo(size.width/2,size.height/2,duration=1)
#鼠标相对移动,周期1秒
pyautogui.moveRel(100,0,duration=1)
实时获取鼠标位置
import pyautogui
#上一次鼠标位置
last_pos = pyautogui.position()
try:
while pyautogui.position().x !=0:
#新位置
new_pos = pyautogui.position()
if last_pos!= new_pos:
print(new_pos)
last_pos = new_pos
except KeyboardInterrupt:
print('\nExit.')
Point(x=16, y=24)
Point(x=15, y=23)
Point(x=15, y=22)
Point(x=15, y=21)
Point(x=14, y=20)
Point(x=14, y=19)
Point(x=13, y=18)
Point(x=13, y=16)
Point(x=13, y=15)
Point(x=12, y=14)
Point(x=12, y=13)
Point(x=11, y=12)
Point(x=11, y=11)
Point(x=11, y=10)
Point(x=11, y=9)
Point(x=11, y=8)
Point(x=10, y=8)
Point(x=9, y=6)
Point(x=9, y=5)
Point(x=9, y=4)
Point(x=9, y=3)
Point(x=9, y=2)
Point(x=8, y=1)
Point(x=8, y=0)
Point(x=7, y=0)
Point(x=6, y=0)
Point(x=5, y=0)
Point(x=4, y=0)
Point(x=3, y=0)
Point(x=2, y=0)
Point(x=1, y=0)
鼠标移动加点击
import pyautogui
import time
#系统准备时间
time.sleep(0)
#通过file图片获取file位置
file_pos = pyautogui.locateOnScreen('btn_file.png')
#获取正中间位置
goto_pos = pyautogui.center(file_pos)
#移动鼠标
pyautogui.moveTo(goto_pos,duration=1)
#点击
pyautogui.click()
#再移动鼠标
pyautogui.moveRel(0,200,duration=1)
键盘输入
import pyautogui
import time
#系统准备时间
time.sleep(1)
#点击一次编辑器
pyautogui.click(button='left')
#输入【I LIKE PYTHON】
pyautogui.typewrite('I LIKE PYTHON')
#输入回车,然后慢速输入内容(内容,周期)
pyautogui.typewrite('\nI LIKE PYTHON TOO.',0.25)
#输入【good】,然后将头文字改为大写G,最后在行尾写个句号
pyautogui.typewrite(['enter','g','o','o','d','left','left','left','backspace','G','end','.'],0.25)
组合键输入
import pyautogui
import time
time.sleep(2)
#每个动作间隔0.5秒
pyautogui.PAUSE = 0.5
#安全失败退出
#pyautogui.FAILSAFE = True
#记事本打出时间
pyautogui.press("f5");
#打入三行内容
pyautogui.typewrite('\nhello')
pyautogui.typewrite('\nhello')
pyautogui.typewrite('\nhello')
#按下Ctrl键
pyautogui.keyDown("ctrl");
#按下a键,全选
pyautogui.press('a');
#按下c键,复制
pyautogui.press('c');
#松开Ctrl键
pyautogui.keyUp('ctrl');
#鼠标点击记事本下方
pyautogui.click(600,600)
#输入两个空行
pyautogui.typewrite('\n\n')
#粘贴
pyautogui.hotkey('ctrl','v')