Python查找两个鼠标位置之间的差异

2024-03-29 08:57:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找出两个鼠标位置之间的区别。虽然,我不明白,我是一个初学者,不知道是否有人可以帮我。你知道吗

我试着制作一个程序,打印出你在x时间内移动鼠标的距离。这是我唯一搞不懂的部分。你知道吗

from tkinter import *
import time

time.sleep(1)
x = (pyautogui.position())

time.sleep(1)
y = (pyautogui.position())

p = x - y
print(p)

我希望它能打印出差异,但它给了我一个错误。你知道吗

TypeError: unsupported operand type(s) for -: 'Point' and 'Point'

Tags: fromimport程序距离timetkinter时间position
2条回答

请看一下documentation。如果要获得单独的坐标,必须将坐标存储在两个对象中:

x, y = pyautogui.position()

现在你可以用简单的算法得到距离向量:

time.sleep(1)
x0, y0 = (pyautogui.position())

time.sleep(1)
x1, y1 = (pyautogui.position())

Distance_X = x1 - x0
Distance_Y = y1 - y0

否则,必须使用xy对象的成员。你知道吗

time.sleep(1)
P0 = (pyautogui.position())

time.sleep(1)
P1 = (pyautogui.position())

Distanxe_X = P1.x - P0.x
Distance_Y = P1.y - P0.y

pyautogui返回一个Point,它是形式为(x,y)的二维坐标

您需要使用Distance Formula来计算任意两点之间的距离

工作示例如下

import time
import pyautogui
import math

time.sleep(1)
x = (pyautogui.position())

time.sleep(1)
y = (pyautogui.position())

dist = math.sqrt((y.x - x.x)**2 + (y.y - x.y)**2)

print(round(dist, 2))

而且,pyautogui不是python3中tkinter模块的一部分。所以我已经分别安装和导入了它。你知道吗

相关问题 更多 >