📅  最后修改于: 2023-12-03 15:33:49.436000             🧑  作者: Mango
在编写 Python 应用程序时,获取鼠标坐标是一个常见的需求。在本文中,我将介绍如何使用 Python 获取鼠标坐标。在这里,我将使用 pyautogui 模块。
在开始之前,您需要安装 pyautogui 模块。您可以通过运行以下命令来安装该模块:
pip install pyautogui
下面是获取鼠标坐标的 Python 代码:
import pyautogui
while True:
x, y = pyautogui.position()
position_str = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(position_str, end='')
print('\b' * len(position_str), end='', flush=True)
代码分解:
在运行上面的代码后,您将会在控制台中看到实时的鼠标坐标。
在这篇文章中,我们介绍了如何使用 pyautogui 模块来获取鼠标坐标。pyautogui 是一个用于 GUI 自动化的 Python 模块,它提供了一组非常有用的函数,可以让您从屏幕上获取各种信息。希望这篇文章对您有所帮助。