如何在Python中控制鼠标光标,即移动到特定位置并单击,在Windows下?
当前回答
另一种选择是鼠标库,我个人使用它,因为它相对简单和跨平台。
下面是你如何使用它:
import mouse
# move 100 right and 100 down with a duration of 0.5 seconds
mouse.move(100, 100, absolute=False, duration=0.5)
# left click
mouse.click('left')
# right click
mouse.click('right')
这是来源:如何在Python中控制鼠标
其他回答
如果你想移动鼠标,使用这个:
import pyautogui
pyautogui.moveTo(x,y)
如果你想点击,使用这个:
import pyautogui
pyautogui.click(x,y)
如果你没有安装pyautogui,你必须将python附加到CMD。进入CMD并输入:pip install pyautogui
这将为Python 2.x安装pyautogui。
对于Python 3。X,你可能不得不使用pip3 install pyautogui或python3 -m PIP install pyautogui。
Pynput是我发现的最好的解决方案,无论是Windows还是Mac。超级容易编程,工作得非常好。
例如,
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)
另一种选择是鼠标库,我个人使用它,因为它相对简单和跨平台。
下面是你如何使用它:
import mouse
# move 100 right and 100 down with a duration of 0.5 seconds
mouse.move(100, 100, absolute=False, duration=0.5)
# left click
mouse.click('left')
# right click
mouse.click('right')
这是来源:如何在Python中控制鼠标
尝试Clicknium, https://www.clicknium.com/documents/references/python/mouse/ 它可以控制鼠标和键盘,并帮助您定位web浏览器和桌面应用程序中的UI元素。
这是一个沿着圆圈移动鼠标的示例
from time import sleep
import math
from clicknium import clicknium as cc
def circle():
a,b = cc.mouse.position()
w = 20
m = (2*math.pi)/w
r = 200
while 1:
for i in range(0, w+1):
x = int(a+r*math.sin(m*i))
y = int(b+r*math.cos(m*i))
cc.mouse.move(x,y)
sleep(0.2)
if __name__ == "__main__":
circle()
Linux
from Xlib import X, display
d = display.Display()
s = d.screen()
root = s.root
root.warp_pointer(300,300)
d.sync()
来源:Python鼠标移动5行代码(仅适用于Linux)。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录