首页 > 编程语言 >[python]《Python编程快速上手让繁琐工作自动化》学习笔记7

[python]《Python编程快速上手让繁琐工作自动化》学习笔记7

时间:2022-12-18 23:25:48浏览次数:68  
标签:pyautogui sudo 鼠标 python 编程 Python install 模拟 positionStr

date: 2019-08-02 16:24:19 +0800 
tags: 
    - Python

1. 用GUI 自动化控制键盘和鼠标第18章 (代码下载)

pyautogui模块可以向Windows、OS X 和Linux 发送虚拟按键和鼠标点击。根据使用的操作系统,在安装pyautogui之前,可能需要安装一些其他模块。

  • Windows: 不需要安装其他模块。
  • OS X:
sudo pip3 install pyobjc-framework-Quartz
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc
  • Linux:
sudo pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev

依赖项安装后安装pyautogui:

pip install pyautogui

使用pyautogui需要注意防止或恢复GUI。通过ctrl+cg关闭程序或者。设定暂停和自动防故障,如下:

# 在每次函数调用后等一会儿,将pyautogui.PAUSE 变量设置为要暂停的秒数
pyautogui.PAUSE = 1
# 设定自动防故障
pyautogui.FAILSAFE = True

常用函数如下:

函数 用途
moveTo(x,y,duration) 将鼠标移动到指定的x、y 坐标,duration每次移动耗时多少秒,如果没有数调用duration,鼠标就会马上从一个点移到另一个点
moveRel(xOffset,yOffset,duration) 相对于当前位置移动鼠标
pyautogui.position() 确定鼠标当前位置
dragTo(x,y,duration) 按下左键移动鼠标
dragRel(xOffset,yOffset,duration) 按下左键,相对于当前位置移动鼠标
click(x,y,button) 模拟点击(默认是左键)
rightClick() 模拟右键点击
middleClick() 模拟中键点击
doubleClick() 模拟左键双击
pyautogui.scroll(units) 模拟滚动滚轮。正参数表示向上滚动,负参数表示向下滚动
pyautogui.screenshot() 返回屏幕快照的Image 对象
pyautogui.pixelMatchesColor(x,y,tuple) 判断x、y 坐标处的像素与指定的颜色是否匹配。第一和第二个参数是xy整数坐标。第三个参数是包含3个整数元组,表示RGB 颜色
pyautogui.locateOnScreen(image) 将返回图像iamge所在处当前屏幕图像的坐标,如果无法匹配返回None
pyautogui.center(x,y,w,h) 返回区域的中心坐标
typewrite(message) 键入给定消息字符串中的字符
typewrite([key1,key2,key3]) 键入给定键字符串,pyautogui.KEYBOARD_KEYS查看键字符串的列表
press(key) 按下并释放给定键
keyDown(key) 模拟按下给定键
keyUp(key) 模拟释放给定键
mouseDown(x,y,button) 模拟在x、y 处按下指定鼠标按键
mouseUp(x,y,button) 模拟在x、y 处释放指定键
hotkey([key1,key2,key3]) 模拟按顺序按下给定键字符串,然后以相反的顺序释放

2 项目练习

2.1 项目:“现在鼠标在哪里?”

在移动鼠标时随时显示 x y 坐标和改点RGB值

# 显示鼠标实时位置
import pyautogui

# 程序终止crtl+c
print('Press Ctrl-C to quit.')

try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        # 获得当前点的颜色
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3)
        positionStr += ', ' + str(pixelColor[1]).rjust(3)
        positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')'
        # end结尾添加的字符,默认换行符
        print(positionStr, end='')
        # https://blog.csdn.net/lucosax/article/details/34963593
        # 得到了许多\b 字符构成的字符串,长度与positionStr中保存的字符串长度一样,效果就是擦除了前面打印的字符串\
        # flush把文件从内存buffer(缓冲区)中强制刷新到硬盘中,同时清空缓冲区。实现动态效果
        print('\b' * len(positionStr), end='', flush=True)

# 退出程序
except KeyboardInterrupt:
    print('\nDone.')

2.2 项目:自动填表程序

实现自动填表,表格网站,地址http://autbor.com/form

import pyautogui, time
# Set these to the correct coordinates for your computer
# name输入位置
nameField = (622, 199)
submitButton = (601, 978)
submitButtonColor = (31, 115, 230)
submitAnotherLink = (760, 224)

# 表单数据
formData = [{'name': 'Alice', 'fear': 'eavesdroppers', 'source': 'wand','robocop': 4, 'comments': 'Tell Bob I said hi.'},
            {'name': 'Bob', 'fear': 'bees', 'source': 'amulet', 'robocop': 4,'comments': 'Please take the puppets out of the break room.'},
            {'name': 'Carol', 'fear': 'puppets', 'source': 'crystal ball','robocop': 1, 'comments': 'Please take the puppets out of the break room.'},
            {'name': 'Alex Murphy', 'fear': 'ED-209', 'source': 'money','robocop': 5, 'comments': 'Protect the innocent. Serve the publictrust. Uphold the law.'},]

# 暂停0.5s
pyautogui.PAUSE = 0.5
for person in formData:
    # Give the user a chance to kill the script.
    print('>>> 5 SECOND PAUSE TO LET USER PRESS CTRL-C <<<')
    time.sleep(5)
    # Wait until the form page has loaded.
    # 判断点submitButton和submitButtonColor的颜色是否匹配
    while not pyautogui.pixelMatchesColor(submitButton[0], submitButton[1],submitButtonColor):
        time.sleep(0.5)
    print('Entering %s info...' % (person['name']))
        
    # 从名字开始填写
    pyautogui.click(nameField[0], nameField[1])
    # Fill out the Name field.
    pyautogui.typewrite(person['name'] + '\t')
    # Fill out the Greatest Fear(s) field.
    pyautogui.typewrite(person['fear'] + '\t')
    
    # Fill out the Source of Wizard Powers field.
    if person['source'] == 'wand':
        # 按下键
        pyautogui.typewrite(['down'])
        # 回车
        pyautogui.typewrite(['enter'])
        # tab
        pyautogui.typewrite(['\t'])
    elif person['source'] == 'amulet':
        pyautogui.typewrite(['down', 'down'])
        pyautogui.typewrite(['enter'])
        pyautogui.typewrite(['\t'])
    elif person['source'] == 'crystal ball':
        pyautogui.typewrite(['down', 'down', 'down'])
        pyautogui.typewrite(['enter'])
        pyautogui.typewrite(['\t'])
    elif person['source'] == 'money':
        pyautogui.typewrite(['down', 'down', 'down', 'down'])
        pyautogui.typewrite(['enter'])
        pyautogui.typewrite(['\t'])
    # Fill out the RoboCop field.
    if person['robocop'] == 1:
        pyautogui.typewrite([' ', '\t'])
    elif person['robocop'] == 2:
        pyautogui.typewrite(['right', '\t'])
    elif person['robocop'] == 3:
        pyautogui.typewrite(['right', 'right', '\t'])
    elif person['robocop'] == 4:
        pyautogui.typewrite(['right', 'right', 'right', '\t'])
    elif person['robocop'] == 5:
        pyautogui.typewrite(['right', 'right', 'right', 'right', '\t'])
        
    # Fill out the Additional Comments field.
    # 输入文字
    pyautogui.typewrite(person['comments'] + '\t')
    # Click Submit.
    #pyautogui.press('enter')
    # Wait until form page has loaded.
    print('Clicked Submit.')
    time.sleep(5)
    # Click the Submit another response link.
    # 确认提交
    pyautogui.click(submitAnotherLink[0], submitAnotherLink[1])

3 参考链接

标签:pyautogui,sudo,鼠标,python,编程,Python,install,模拟,positionStr
From: https://www.cnblogs.com/luohenyueji/p/16991246.html

相关文章