Skip to content

Instantly share code, notes, and snippets.

@luzihang123
Created April 23, 2020 02:27
Show Gist options
  • Save luzihang123/fcb9bdea8e9210c307b85a8571712fce to your computer and use it in GitHub Desktop.
Save luzihang123/fcb9bdea8e9210c307b85a8571712fce to your computer and use it in GitHub Desktop.
'''
Created on Apr 7, 2020
@author: admin
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码。
下面的代码让鼠标移到屏幕中央。
'''
import pyautogui, time, pyperclip
from com.fy.utils.file.FileUtils import File_Utils
class PyAutoGUIPlugs:
def __init__(self):
#保护措施,避免失控
pyautogui.FAILSAFE = True
#为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。
pyautogui.PAUSE = 0.5
#鼠标操作
#当前屏幕的分辨率(宽度和高度)
def screeSize(self):
return pyautogui.size()
# (x,y)是否在屏幕上
def onScreen(self, X, Y):
return pyautogui.onScreen(X, Y)
#截取某一区域图;
def screenShot(self, imageFilePath, left, top, width, height):
print("截取图片【开始】。坐标信息为:", left, top, width, height)
fu = File_Utils(imageFilePath)
fu.deleFile()#如果文件存在,则删除截图;
#您可以传递区域左侧,顶部,宽度和高度的四个整数元组来捕获:截图
#左侧:取左侧高中间点出X轴坐标;
#顶部:取顶部宽中间点Y轴坐标;
#宽度:截取部分图片的宽;
#高度:截取部分图片的高度
im = pyautogui.screenshot(region=(left, top, width, height))
# 保存图片
im.save(imageFilePath)
print("截取图片【完毕】...")
# 获取鼠标位置
def get_mouse_positon(self):
time.sleep(5) # 准备时间
print('开始获取鼠标位置')
x = None
y = None
try:
for i in range(5):
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = '鼠标坐标点(X,Y)为:{},{}'.format(str(x).rjust(4), str(y).rjust(4))
pix = pyautogui.screenshot().getpixel((x, y)) # 获取鼠标所在屏幕点的RGB颜色
positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'
print(positionStr)
time.sleep(1) # 停顿时间
del i
except:
print('获取鼠标位置失败')
return x, y
#num_seconds秒钟鼠标移动坐标为X, Y位置 绝对移动,duration为持续时间
def moveTo(self, X, Y, num_seconds):
pyautogui.moveTo(X, Y, duration=num_seconds, tween=pyautogui.easeInOutQuad)
#右击
def rightClick(self, moveToX, moveToY):
pyautogui.rightClick(x=moveToX, y=moveToY)
def middleClick(self, moveToX, moveToY):
pyautogui.middleClick(x=moveToX, y=moveToY)
# 鼠标moveToX, moveToY位置,0间隔,左击两下
def doubleClick(self, moveToX, moveToY):
pyautogui.doubleClick(x=moveToX, y=moveToY, button="left", interval=0.0)
# 鼠标moveToX,moveTo位置左击三下
def tripleClick(self, moveToX, moveToY):
pyautogui.tripleClick(x=moveToX, y=moveToY)
#鼠标移动到moveToX, moveToY位置按下(左键)
def mouseDown(self, moveToX, moveToY):
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
#鼠标移动到( moveToX, moveToY)位置再向上滚动10格
def scrollXY(self, moveToX, moveToY):
pyautogui.scroll(10, x=moveToX, y=moveToY) # 10表示:向上滚动10格;-10表示:向下滚动10格;
pyautogui.scroll()
#amount_to_scroll参数表示滚动的格数
def scroll(self, amount_to_scroll):
pyautogui.scroll(amount_to_scroll) # 10表示:向上滚动10格;-10表示:向下滚动10格;
#鼠标水平滚动(Linux)
def hscroll(self):
pyautogui.hscroll()
#鼠标左右滚动(Linux)
def vscroll(self):
pyautogui.vscroll()
# 按住鼠标左键,把鼠标拖拽到(100, 200)位置
def dragTo(self, moveToX, moveToY):
pyautogui.dragTo(moveToX, moveToY, button='left')
# 按住鼠标左键,用2秒钟把鼠标拖拽到(moveToX, moveToY)位置
def dragToNum(self, moveToX, moveToY, num_seconds):
pyautogui.dragTo(moveToX, moveToY, num_seconds, button='left')
# 按住鼠标左键,用0.2秒钟把鼠标向上拖拽(其中,moveToY为负值;)
def dragRel(self, moveToY, num_seconds):
pyautogui.dragRel(0, moveToY, duration=num_seconds)
#++++++键盘操作+++++我们在pyautogui库对于键盘的使用方法大体如下:++++++++++++++
'''
按键 说明
enter(或return 或 \n) 回车
esc ESC键
shiftleft, shiftright 左右SHIFT键
altleft, altright 左右ALT键
ctrlleft, ctrlright 左右CTRL键
tab (\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, volumedown,volumeup 有些键盘没有
pause PAUSE键
capslock, numlock,scrolllock CAPS LOCK, NUM LOCK, 和 SCROLLLOCK 键
insert INS或INSERT键
printscreen PRTSC 或 PRINT SCREEN键
winleft, winright Win键
command Mac OS X command键
'''
#模拟输入信息,每次输入间隔0.5秒
def typewrite(self, Str):
pyautogui.typewrite(message=Str, interval=0.5)
# PyAutoGUI输入【中文】需要用粘贴实现
# Python 2版本的pyperclip提供中文复制
def paste(self, foo):
pyperclip.copy(foo)
pyautogui.hotkey('ctrl', 'v')
# 按下并松开(轻敲)backspace键
def pressBackspace(self):
pyautogui.press('backspace')
# 按下并松开(轻敲)回车键
def pressEnter(self):
pyautogui.press('enter')
#点击ESC
def pressESC(self):
pyautogui.press('esc')
# 按住shift键
def keyDownShift(self):
pyautogui.keyDown('shift')
# 放开shift键
def keyUpShift(self):
pyautogui.keyUp('shift')
# 模拟组合热键(复制;)
def hotkeyCtrlC(self):
pyautogui.hotkey('ctrl', 'c')
# 组合按键(Ctrl+V),粘贴功能,按下并松开'ctrl'和'v'按键
def hotkeyCtrlV(self):
pyautogui.hotkey('ctrl', 'v')
#同时按下shift和某个数字键;
def keyUpShiftNumber(self, number):
pyautogui.keyDown('shift')
pyautogui.press(str(number))
pyautogui.keyUp('shift') # 输出 $ 符号的按键
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment