python - pyautogui is not updating mouse position on Raspberry - Stack Overflow

admin2025-04-20  2

I try to get the mouse position on my Raspberry 4. I used pyautogui in the past, which was working fine. But on the Raspberry the mouse position returned by pyautogui is stale. Other functions on pyautogui works fine.

Here are some details OS:

PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="/"
SUPPORT_URL=";
BUG_REPORT_URL="/"

A note to the pyautogui installation: when using the recommeded command I get the following error: python3 -m pip install pyautogui error: externally-managed-environment

× 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.

with the installation: sudo pip3 install pyautogui --break-system-packages ... ok - pyautogui-Version: 0.9.54

the installation went ok. Functions works in my python (Python 3.11.2).

example code:

    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")

result: pyautogui : 0.9.54 Screen size: Size(width=1920, height=1080) X: 500 Y: 268 X: 100 Y: 150 End

The result of the mouse position is incorrect - it looks stale, when you put it in a loop.

Any idea or hints are welcome. regards, HEP

I try to get the mouse position on my Raspberry 4. I used pyautogui in the past, which was working fine. But on the Raspberry the mouse position returned by pyautogui is stale. Other functions on pyautogui works fine.

Here are some details OS:

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/"

A note to the pyautogui installation: when using the recommeded command I get the following error: python3 -m pip install pyautogui error: externally-managed-environment

× 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.

with the installation: sudo pip3 install pyautogui --break-system-packages ... ok - pyautogui-Version: 0.9.54

the installation went ok. Functions works in my python (Python 3.11.2).

example code:

    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")

result: pyautogui : 0.9.54 Screen size: Size(width=1920, height=1080) X: 500 Y: 268 X: 100 Y: 150 End

The result of the mouse position is incorrect - it looks stale, when you put it in a loop.

Any idea or hints are welcome. regards, HEP

Share Improve this question edited Feb 14 at 11:07 Ulrich Eckhardt 17.5k5 gold badges31 silver badges60 bronze badges asked Jan 25 at 10:54 HEPHEP 134 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You need while/else condition block.

Snippet:

pyautogui.alert("PyAutoGUI-Alert")
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
    print('\n')

I changed the following setting on the raspberry and all issues with pyautogui are gone.

sudo raspi-config
6 Advanced - Options Configure advanced settings
A6 Wayland -Switch between X and Wayland backends
W1 X11 - Openbox window manager with X11 backend 
转载请注明原文地址:http://anycun.com/QandA/1745106199a90375.html