简体   繁体   English

如何在 Python 3 中找到光标的坐标?

[英]How do you find the coordinates of the cursor in Python 3?

I found this code to be the most helpful to other people:我发现这段代码对其他人最有帮助:

x, y = win32api.GetCursorPos()

When I run this code, I get an error "NameError: name 'win32gui' is not defined on line ##".当我运行此代码时,我收到一个错误“NameError: name 'win32gui' is not defined on line ##”。

When trying to import the module:尝试导入模块时:

import win32gui

I get an error "ImportError: No module named win32gui on line ##".我收到错误“ImportError: No module named win32gui on line ##”。

I tried running:我尝试运行:

pip install win32gui

But it doesn't seem to work.但这似乎不起作用。 How can I use win32gui?如何使用win32gui?

win32gui comes as a part of the external package PyWin32 . win32gui是外部包PyWin32的一部分。 You may download this package to install this module.你可以下载这个包来安装这个模块。

Note that this is not a part of the standart library (a third party library, proceed with caution).请注意,这不是标准库的一部分(第三方库,请谨慎操作)。

You can use a third party module pyautogui, which is installed by: pip install pyautogui您可以使用第三方模块 pyautogui,通过以下方式安装: pip install pyautogui

Code for mouse position:鼠标位置代码:

import pyautogui
print pyautogui.position()

It will return a tuple with your x, y coordinate position它将返回一个带有您的 x、y 坐标位置的元组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM