树莓派4上PyAutoGUI无法实时更新鼠标位置问题求助
我之前在其他设备上用PyAutoGUI获取鼠标位置一直很正常,但在树莓派4上,它返回的鼠标位置总是旧的(像是缓存的stale数据),不过PyAutoGUI的其他功能比如弹窗、移动鼠标都能正常工作,这让我很困惑。
我的环境信息
- 操作系统:Debian 12 (bookworm)
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" NAME="Debian GNU/Linux" VERSION_ID="12" VERSION="12 (bookworm)" VERSION_CODENAME=bookworm ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/" - Python版本:3.11.2
- PyAutoGUI版本:0.9.54
PyAutoGUI安装过程说明
一开始我用官方推荐的命令安装时遇到了外部管理环境的错误:
python3 -m pip install pyautogui
错误提示如下:
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to install.If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
后来我用下面的命令强制安装成功了:
sudo pip3 install pyautogui --break-system-packages
安装完成后,PyAutoGUI的大部分功能都能正常在Python里运行,除了鼠标位置获取的问题。
测试代码与运行结果
我写了一段简单的测试代码:
import pyautogui, sys print("pyautogui : ",pyautogui.__version__) print("Screen size: ",pyautogui.size()) pyautogui.alert("PyAutoGUI-Alert") x, y = pyautogui.position() print("X:",x," Y:",y) pyautogui.moveTo(100, 150) x, y = pyautogui.position() print("X:",x," Y:",y) print("End")
单次运行的结果看起来是正常的:
pyautogui : 0.9.54 Screen size: Size(width=1920, height=1080) X: 500 Y: 268 X: 100 Y: 150 End
但如果把获取鼠标位置的逻辑放到循环里,就会发现返回的位置一直是旧的,不会实时更新我手动移动鼠标后的新位置,完全是stale状态。
有没有大佬遇到过类似的问题?或者能给我一些排查的思路和解决建议?非常感谢!
备注:内容来源于stack exchange,提问作者HEP

