PyAutoGUI——图形用户界面自动化(超级详细)

PyAutoGUI模块非常全的详解 由小盆友制作的非常全的PyAutoGUI模块详解! 阅读详情
AI摘要

本文详细介绍PyAutoGUI库的使用方法,包括安装配置、屏幕尺寸获取、鼠标键盘控制等功能,并提供了丰富的示例代码。

开始之前

在开始之前,要先安装所需要的模块 pyautogui, pyautogui 模块可以向 Windows 发送虚拟按键和鼠标点击。

pip install -i https://pypi.douban.com/simple pyautogui

查看屏幕大小

在这里插入图片描述

pyautogui.size() 可以获得屏幕的宽和高的像素数(整数)。

>>> pyautogui.size()
Size(width=1920, height=1080)
# 也可以直接将宽,高进行保存 x, y = pyautogui.size()

获取当前鼠标的位置

函数返回当前位置 X,Y 坐标的元组,如果设置 x,y 的参数,则会覆盖返回结果。

>>> pyautogui.position(x=None, y=None)
Point(x=1437, y=817)

检查指定坐标是否在屏幕上

要检查 X,Y 坐标是否在屏幕上,将它们(支持两个整数,列表,元组)传递给 onScreen() 函数,如果它们在屏幕的边界内,则返回 True ,否则返回 False 。

>>> pyautogui.size()
(1920, 1080)
>>> pyautogui.onScreen(1920, 1080)
False
>>> pyautogui.onScreen(1919, 1079)
True

设置暂停时间

使用 pyautogui.PAUSE 变量设置要暂 停的秒数。如,在设置 pyautogui.PAUSE = 1 之后,每个 PyAutoGUI 函数调用 在执行动作之后,都会等待一秒。

>>> pyautogui.PAUSE = 1


鼠标功能

移动鼠标

移动鼠标到指定位置。

pyautogui.moveTo(x=None, y=None, duration=0.0) # 移动鼠标到指定位置
pyautogui.move(xOffset=None, yOffset=None, duration=0.0) # 在PyAutoGUI 1.0,move 替换了 moveRel,但moveRel也可以使用

pyautogui.moveTo(200, 300, duration=0.5)   # 移动到坐标(200,300)
pyautogui.move(200, 300, duration=0.5)     # 从当前鼠标位置向右移动 200 向下移动 300

主要参数参数:

x/xOffset:X轴坐标,moveTo() 中取值0或正整数值,move()/moveRel() 中取值可以为负数,代表以当前位置向左移动

y/yOffset:Y轴坐标,moveTo() 中取值0或正整数值,move()/moveRel() 中取值可以为负数,代表以当前位置向上移动

duration:移动到指定坐标花费的时间,默认为0,代表立即完成,单位为s


鼠标点击

使用 click() 完成鼠标点击操作。

>>> pyautogui.click(x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0)

主要参数:

x:X轴坐标,默认为 None,即当前X轴坐标

y:Y轴坐标,默认为 None,即当前Y轴坐标(注:X,Y 轴的坐标只能同时指定,不能只指定某一个)

clicks:鼠标的点击次数,默认为 1 次

interval:int 或浮点数,表示每次单击之间需要等待多少秒,默认值为0.0,表示单击之间没有暂停

button:可选 LEFT、MIDDLE、RIGHT、PRIMARY(左键)或 SECONDARY(右键)。它的默认值是 PRIMARY

duration:如果指定了 X,Y 的值,并且该坐标不是鼠标当前位置的话,那么 duration 参数就可以生效,它表示移动到指定坐标花费的时间,默认为 0,表示立即移动。

💬 其他点击操作:

pyautogui.mouseDown()    # 按下鼠标按键(左键)
pyautogui.mouseUp()      # 释放鼠标按键(左键)

鼠标拖动

按住鼠标的某个键进行拖动。

pyautogui.dragTo(x
Python-PyAutoGUI 库详解 PyAutoGUI 是 Python 生态中 ​。 阅读详情

相关推荐

一个神奇的GUI自动化测试库-PyAutoGui

看到这个库的时候,单单从名字就感觉到两个字“简单”,特别容易理解它是用来做什么的,深入了解之后感觉的还是两个字“简单”。这对自动化测试来说便是一个天大的乐事,因为很多工具庞大复杂,仅仅是写一个hello world需要的环境,需要了解的东西比c++,java还难。 PyAutoGUI作为GUI自动化神器,简单高效、函数分类清晰,它被awesome-python、awesome-python-cn收录 现在我就带你们走进PyAutoGUI~ 1. PyAutoGui是什么? PyAutoGui是一

白天像蚂蚁一样工作,晚上像蝴蝶一样生活 7804

【Python自动化】使用pyautogui和selenium对客户端和网页端进行自动化操作(自动化测试)

前言 本身是做数据科学这方面,因部门急接了个自动化测试脚本的活儿,部门没人会用python,故被领导找到,应要求学习了一下客户端和网页端的自动化测试操作,在此记录一下大概流程和注意点,供自己今后参考和备忘,不严谨勿喷。 1. Pyautogui图形用户界面自动化操作 Python的自动化测试包有很多,因时间有限,选择pyautogui,直接通过屏幕定位来操作鼠标和键盘,不用抓取窗口句柄等结构,简单粗暴方便。 这里以win10系统自动操作PLSQL数据库软件为例: # -*- coding: utf-8 -*

weixin_43627748的博客 4749

模拟鼠标拖动操作的方法

鼠标拖动是计算机用户在图形界面下常用的交互操作之一,它可以实现元素的移动、排序、重组等功能。在Python中,我们可以使用第三方库PyAutoGUI来模拟鼠标拖动操作。需要注意的是,PyAutoGUI库是通过控制鼠标的绝对位置来实现模拟操作的,因此在运行代码时请确保鼠标不会被阻挡或移动到其他位置,以免影响操作的准确性。方法将鼠标移动到目标位置,这里的目标位置是通过起始位置的坐标加上一个偏移量得到的。库,并设置了鼠标移动的延迟时间,以便我们能够更清楚地观察到鼠标的移动过程(这一步是可选的)。

uote_e的博客 548

如何在iPhone上本地运行SmolVLM?5亿参数模型实战指南(附Swift代码)

本文详细介绍了如何在iPhone上本地部署和运行轻量高效的SmolVLM多模态模型。通过实战指南,分享了使用Swift和MLX框架进行环境配置、模型加载、内存优化以及实现实时图像分析与文本生成的完整流程,帮助开发者在移动端高效运行AI视觉语言模型。

echo99的专栏 654

飞腾D2000开发板实战:手把手教你为UEFI/EDK2驱动SD3077 RTC时钟芯片

本文详细介绍了在飞腾D2000开发板上为UEFI/EDK2环境驱动SD3077 RTC时钟芯片的全流程开发指南。从硬件连接到软件适配,包括引脚复用配置、电源管理优化以及UEFI驱动的深度定制,帮助开发者解决嵌入式系统中的时间同步问题,确保RTC芯片的稳定性和准确性。

weixin_30892037的博客 423

霍尔编码器 vs 磁编码器:如何为你的电机控制项目选择最佳传感器(附实战对比)

本文深入对比了霍尔编码器和磁编码器在电机控制项目中的应用,从技术原理、性能参数、成本效益到实际场景选择提供了全面分析。通过实验室测试和项目案例,帮助工程师根据精度、环境抗性和预算选择最佳传感器方案,特别适合机器人、伺服驱动等领域的开发者参考。

weixin_28312391的博客 352

【Labview】对连续采集数据的一种采样方法

图1如图1,想要对这样一个连续信号进行数据点的采集,我根据网上收集到的方法,在此分享一下最终的结果。可以拓展结合文件的写入,完成对连续信号的采集和输出。 已知: 一个连续信号,如图1. 目标: 设定一定采集点个数、采集点时间间隔、和采集周期次数,得到这条连续信号的数据点,如图2. 图2 方法如下: 主要利用的是队列的方法。 在连续信号生成的时候,不断的写入队列,并且清除队列。 ...

李时珍食疗志的博客 1万+

在Mac上搭建本地AI工作流:Dify与DeepSeek的完美结合

本文介绍了在Mac上搭建本地AI工作流的方法,结合Dify平台和DeepSeek模型。Dify是一个开源的大语言模型应用开发平台,支持可视化AI应用开发。部署步骤包括:1)安装Homebrew和Docker;2)克隆Dify代码库;3)配置环境变量并启动服务。同时,使用Ollama工具本地部署DeepSeek模型,将其集成到Dify中创建聊天助手。通过配置模型接口和新建应用,用户可以在本地环境中实现AI对话功能。这种方案既保证了数据隐私,又提供了灵活的AI应用开发能力。

Linda的自动化测试之旅 1535

【x265编码器】章节1——x265的lookahead模块分析

目录一、模块功能二、lookahead模块分析1.调用流程2.代码分析Lookahead::addPicture()Lookahead::checkLookaheadQueue()Lookahead::getDecidedPicture()Lookahead::findJob()Lookahead::slicetypeDecide()PreLookaheadGroup::processTasksLookaheadTLD::lowresIntraEstimate()Lookahead::slicetypeAn

vcvdv123的博客 4万+

YOLOv8改进 | Conv篇 | DWRSeg扩张式残差助力小目标检测 (附修改后的C2f+Bottleneck)

本文内容给大家带来的DWRSeg中的DWR模块来改进YOLOv8中的C2f和Bottleneck模块,主要针对的是小目标检测,主要创新点可以总结如下:多尺度特征提取机制的深入研究和创新的DWR模块和SIR模块的提出这种方法使得网络能够更灵活地适应不同尺度的特征,从而更准确地识别和分割图像中的物体。通过本文你能够了解到DWRSeg的基本原理和框架,并且能够在你自己的网络结构中进行添加(DWRSeg需要增加一定的计算量一个DWR模块大概增加0.4GFLOPs)。

Snu77的博客 9571

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

上一篇: pymysql连接数据库报错:TypeError: __init__() takes 1 positional argument but 5 were given
下一篇: 数据分析——常见数据指标汇总
Dream丶Killer
Dream丶Killer 优质创作者: python技术领域 优质创作者: python技术领域
博客等级 码龄8年 7万+粉丝 187原创
评论 70
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dream丶Killer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值