328 views|4 replies

539

Posts

3

Resources
The OP
 

Reading notes for the book "Python Programming Quick Start (2nd Edition)" 11. Using GUI Automation to Control the Keyboard and Mouse (1) [Copy link]

This post was last edited by xinmeng_wit on 2024-6-5 21:50

This is the last chapter of this book. This chapter mainly introduces how to use Python to control the computer keyboard and mouse.

Similarly, you need to install the specified module first. The name of the module is PyAutoGUI. The installation process can refer to the python module installation method in the previous chapter. You can use pip to install it, which is omitted here.

First, the PyAutoGUI module can obtain the size information of the current computer screen, including the width and height of the screen, through the pyautogui.size() method.

import pyautogui

print(pyautogui.size())

operation result:

Size(width=1366, height=768)

The display screen is 1366 * 768

If you want to move the mouse pointer, you can use the pyautogui.moveTo() method and the pyautogui.move() method. The moveTo() method passes in the coordinates of the target position on the screen (x, y) and the speed of movement (duration). The move() method is similar, the difference is that move is how many pixels are moved relative to the current position, not the absolute position.

import pyautogui

# 打印屏幕尺寸
print(pyautogui.size())

for i in range(10):
    pyautogui.moveTo(100, 100, 0.25) # 鼠标移动到(100,100)的位置,移动时间0.25s
    pyautogui.moveTo(200, 100, 0.25) # 鼠标移动到(200,100)的位置,移动时间0.25s
    pyautogui.moveTo(200, 200, 0.25) # 鼠标移动到(200,200)的位置,移动时间0.25s
    pyautogui.moveTo(100, 200, 0.25) # 鼠标移动到(100,200)的位置,移动时间0.25s

for i in range(10):
    pyautogui.move(100, 0, 0.25) # 鼠标右移100像素点
    pyautogui.move(0, 100, 0.25) # 鼠标下移100像素点
    pyautogui.move(-100, 0, 0.25) # 鼠标左移100像素点
    pyautogui.move(0, -100, 0.25) # 鼠标上移100像素点

After running, you can see the mouse moving automatically on the screen.

In addition, you can also get the current coordinates of the mouse pointer through the position() method.

import pyautogui

# 使用position()获取x,y的坐标
print(f'鼠标位置: {pyautogui.position()}')
# 使用.x获取坐标的x值
print(f'鼠标x位置: {pyautogui.position().x}')
# 使用下标获取坐标的y值
print(f'鼠标y位置: {pyautogui.position()[1]}')

operation result:

Latest reply

Looking forward to your follow-up article!!! I'll make progress with you every day... Hahahaha   Details Published on 2024-6-7 08:50
 
 

1129

Posts

1

Resources
2
 

Use pip install pyautogui as agi

Then I tried it, it works fine.

Thank you, brother!

Comments

It is really useful. There are more powerful functions later. My hands were shaking when I clicked publish yesterday, so I only wrote a small part.  Details Published on 2024-6-6 19:21
 
 
 

539

Posts

3

Resources
3
 
hellokitty_bean posted on 2024-6-6 14:53 Use pip to install pyautogui as agi and then try it out. It works great. Thank you, brother!

It is really useful. There are more powerful functions later. My hands were shaking when I clicked publish yesterday, so I only wrote a small part.

Comments

Looking forward to your follow-up good articles!!! I will make progress with you day by day... Hahahaha  Details Published on 2024-6-7 08:50
 
 
 

1129

Posts

1

Resources
4
 
xinmeng_wit posted on 2024-6-6 19:21 It is really easy to use, and there are more powerful functions later. My hands were shaking when I clicked the publish button yesterday, so I only wrote a small part

Looking forward to your follow-up article!!!

I'll make progress with you every day... Hahahaha

 
 
 

539

Posts

3

Resources
5
 
hellokitty_bean posted on 2024-6-7 08:50 I look forward to your follow-up good articles!!! I will make progress with you day by day... Hahaha

One step at a time, haha, good words,

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list