mirror of
https://github.com/hanzoai/autogui.git
synced 2026-10-30 18:47:06 +00:00
52 KiB
52 KiB
In [ ]:
import pyperclip
import pyautogui
# PyAutoGUI中文输入需要用粘贴实现
# Python 2版本的pyperclip提供中文复制
def paste(foo):
pyperclip.copy(foo)
pyautogui.hotkey('ctrl', 'v')
foo = u'学而时习之'
# 移动到文本框
pyautogui.click(130,30)
paste(foo)In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
# 鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
# use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
# 在每次输入之间暂停0.25秒
pyautogui.typewrite('Hello world!', interval=0.25)
pyautogui.press('esc')
pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')In [ ]:
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration=0.5) # 向右
distance -= 5
pyautogui.dragRel(0, distance, duration=0.5) # 向下
pyautogui.draIn gRel(-distance, 0, duration=0.5) # 向左
distance -= 5
pyautogui.dragRel(0, -distance, duration=0.5) # 向上In [ ]:
import pyautogui
pyautogui.FAILSAFE = FalseIn [ ]:
import pyautogui
pyautogui.PAUSE = 2.5
pyautogui.moveTo(100,100); pyautogui.click()In [ ]:
import pyautogui
# 当前鼠标的坐标
pyautogui.position()(123, 372)
In [ ]:
# 当前屏幕的分辨率(宽度和高度)
pyautogui.size()(1920, 1080)
In [ ]:
# (x,y)是否在屏幕上
x, y = 122, 244
pyautogui.onScreen(x, y)True
In [ ]:
import pyautogui
pyautogui.PAUSE = 2.5In [ ]:
import pyautogui
pyautogui.FAILSAFE = TrueIn [ ]:
num_seconds = 1.2
# 用num_seconds秒的时间把光标移动到(x, y)位置
pyautogui.moveTo(x, y, duration=num_seconds)
# 用num_seconds秒的时间把光标的X轴(水平)坐标移动xOffset,
# Y轴(竖直)坐标向下移动yOffset。
xOffset, yOffset = 50, 100
pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)In [ ]:
pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')In [ ]:
pyautogui.rightClick(x=moveToX, y=moveToY)
pyautogui.middleClick(x=moveToX, y=moveToY)
pyautogui.doubleClick(x=moveToX, y=moveToY)
pyautogui.tripleClick(x=moveToX, y=moveToY)In [ ]:
pyautogui.scroll(clicks=amount_to_scroll, x=moveToX, y=moveToY)In [ ]:
pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')In [ ]:
# 每次键入的时间间隔
secs_between_keys = 0.1
pyautogui.typewrite('Hello world!\n', interval=secs_between_keys)In [ ]:
pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)In [ ]:
pyautogui.KEYBOARD_KEYS[:10]['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&']
In [ ]:
pyautogui.hotkey('ctrl', 'a') # 全选
pyautogui.hotkey('ctrl', 'c') # 复制
pyautogui.hotkey('ctrl', 'v') # 粘贴In [ ]:
pyautogui.keyDown(key_name)
pyautogui.keyUp(key_name)In [ ]:
pyautogui.alert('这个消息弹窗是文字+OK按钮')
pyautogui.confirm('这个消息弹窗是文字+OK+Cancel按钮')
pyautogui.prompt('这个消息弹窗是让用户输入字符串,单击OK')''
In [ ]:
# 返回一个Pillow/PIL的Image对象
pyautogui.screenshot()
pyautogui.screenshot('foo.png')In [ ]:
# 返回(最左x坐标,最顶y坐标,宽度,高度)
pyautogui.locateOnScreen('pyautogui/looks.png')(0, 1040, 48, 40)
In [ ]:
for i in pyautogui.locateAllOnScreen('pyautogui/looks.png'):
print(i)(0, 1040, 48, 40)
In [ ]:
list(pyautogui.locateAllOnScreen('pyautogui/looks.png'))[(0, 1040, 48, 40)]
In [ ]:
pyautogui.locateCenterOnScreen('pyautogui/looks.png')(24, 1060)
In [ ]:
pyautogui.size()(1920, 1080)
In [ ]:
pyautogui.position()(272, 688)
In [ ]:
# ! python 3
import pyautogui
print('Press Ctrl-C to quit')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: {} Y: {}'.format(*[str(x).rjust(4) for x in [x, y]])
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')In [ ]:
# ! python
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print positionStr,
print '\b' * (len(positionStr) + 2),
sys.stdout.flush()
except KeyboardInterrupt:
print '\n'In [ ]:
import pyautogui
pyautogui.onScreen(0, 0)True
In [ ]:
pyautogui.onScreen(0, -1)False
In [ ]:
pyautogui.onScreen(0, 2080)False
In [ ]:
pyautogui.onScreen(1920, 1080)False
In [ ]:
pyautogui.onScreen(1919, 1079)True
In [ ]:
pyautogui.moveTo(100, 200) # 光标移动到(100, 200)位置
pyautogui.moveTo(None, 500) # 光标移动到(100, 500)位置
pyautogui.moveTo(600, None) # 光标移动到(600, 500)位置In [ ]:
pyautogui.moveTo(100, 200, duration=2) # 用2秒把光标移动到(100, 200)位置In [ ]:
pyautogui.moveTo(100, 200) #把光标移动到(100, 200)位置
pyautogui.moveRel(0, 50) #向下移动50
pyautogui.moveRel(30, 0, 2) #向右移动30
pyautogui.moveRel(30, None) #向右移动30In [ ]:
# 按住鼠标左键,把鼠标拖拽到(100, 200)位置
pyautogui.dragTo(100, 200, button='left')
# 按住鼠标左键,用2秒钟把鼠标拖拽到(300, 400)位置
pyautogui.dragTo(300, 400, 2, button='left')
# 按住鼠标右键,用2秒钟把鼠标拖拽到(30,0)位置
pyautogui.dragTo(30, 0, 2, button='right')In [ ]:
# 开始很慢,不断加速
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)
# 开始很快,不断减速
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)
# 开始和结束都快,中间比较慢
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)
# 一步一徘徊前进
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)
# 徘徊幅度更大,甚至超过起点和终点
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)In [ ]:
pyautogui.click()Warning:
Output truncated. This notebook contains too many cells to display efficiently.