Python pyautogui

安装

pip3 install pyautogui

原文:https://blog.csdn.net/weixin_43430036/article/details/84650938

pyautogui鼠标操作样例

import pyautogui

# 获取当前屏幕分辨率
screenWidth, screenHeight = pyautogui.size()

# 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()

# 2秒钟鼠标移动坐标为100,100位置  绝对移动
#pyautogui.moveTo(100, 100,2)
pyautogui.moveTo(x=100, y=100,duration=2, tween=pyautogui.linear)

#鼠标移到屏幕中央。
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

# 鼠标左击一次
#pyautogui.click()
# x 
# y 
# clicks 点击次数
# interval点击之间的间隔
# button 'left', 'middle', 'right' 对应鼠标 左 中 右或者取值(1, 2, or 3)
# tween 渐变函数
#
pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠标相对移动 ,向下移动
#pyautogui.moveRel(None, 10)
pyautogui.moveRel(xOffset=None, yOffset=10,duration=0.0, tween=pyautogui.linear)


# 鼠标当前位置0间隔双击
#pyautogui.doubleClick()
pyautogui.doubleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

# 鼠标当前位置3击
#pyautogui.tripleClick()
pyautogui.tripleClick(x=None, y=None, interval=0.0, button='left', duration=0.0, tween=pyautogui.linear)

#右击
pyautogui.rightClick()

#中击
pyautogui.middleClick()

#  用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
#  use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(x=500, y=500, duration=2, tween=pyautogui.easeInOutQuad)

#鼠标拖拽
pyautogui.dragTo(x=427, y=535, duration=3,button='left')

#鼠标相对拖拽
pyautogui.dragRel(xOffset=100,yOffset=100,duration=,button='left',mouseDownUp=False)

#鼠标移动到x=1796, y=778位置按下
pyautogui.mouseDown(x=1796, y=778, button='left')

#鼠标移动到x=2745, y=778位置松开(与mouseDown组合使用选中)
pyautogui.mouseUp(x=2745, y=778, button='left',duration=5)

#鼠标当前位置滚轮滚动
pyautogui.scroll()
#鼠标水平滚动(Linux)
pyautogui.hscroll()
#鼠标左右滚动(Linux)
pyautogui.vscroll()

pyautogui键盘操作样例

#模拟输入信息
pyautogui.typewrite(message='Hello world!',interval=0.5)
#点击ESC
pyautogui.press('esc')
# 按住shift键
pyautogui.keyDown('shift')
# 放开shift键
pyautogui.keyUp('shift')
# 模拟组合热键
pyautogui.hotkey('ctrl', 'c')

按键支持

按键 说明
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键

提示信息

alert

#pyautogui.alert('This is an alert box.','Test')
pyautogui.alert(text='This is an alert box.', title='Test')

option

#pyautogui.confirm('Shall I proceed?')
pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])

password

a = pyautogui.password('Enter password (text will be hidden)')
print(a)

prompt

a = pyautogui.prompt('input  message')
print(a)

截屏

整个屏幕截图并保存

im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')

im2 = pyautogui.screenshot('my_screenshot2.png')

屏幕查找图片位置并获取中间点

#在当前屏幕中查找指定图片(图片需要由系统截图功能截取的图)
coords = pyautogui.locateOnScreen('folder.png')
#获取定位到的图中间点坐标
x,y=pyautogui.center(coords)
#右击该坐标点
pyautogui.rightClick(x,y)

安全设置

import pyautogui

#保护措施,避免失控
pyautogui.FAILSAFE = True
#为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。
pyautogui.PAUSE = 0.5

中文输出

当你使用 pyautogui.write() 里面是中文,是无法输出的。

这时你需要导入一个库:pyperclip 。

这个库中有一个方法 copy 也就是复制,比如我想复制中文。

import pyperclip

content = "你好,世界!"
pyperclip.copy(content)

再利用,pyautogui 的组合键,也就是 ctrl + v 进行粘贴。

远程桌面失效

远程桌面关闭后模拟鼠标键盘失效的问题:

原因分析

因为通过mstsc启动远程桌面连接时,被连接的Windows会启动一个会话(Session)。此时你对远程桌面窗口里面的所有操作(鼠标,键盘)将会‘翻译’成TCP包传输过去,被连接的Windows接收到这些包之后,‘还原’命令并且在当前的活动会话上面执行。而当你断开连接时(点X关闭),会话变成断开状态,Windows会自动关闭会话(事实上会话还是在的,只是状态变成断开的),也就导致了所有基于GUI的操作‘失效’了。

解决方案的脚本命令

@%windir%\System32\tscon.exe 0 /dest:console
@%windir%\System32\tscon.exe 1 /dest:console
@%windir%\System32\tscon.exe 2 /dest:console

把上面的这个脚本命令保存为.bat批处理文件,放到桌面上,每次想断开远程连接的时候,点击运行即可。

猜你喜欢

转载自www.cnblogs.com/py-peng/p/12773607.html