RDP 上的 Pyautogui

Pyautogui on RDP

我在 Windows 10 和 Python 3 中使用 pyautogui,在远程桌面访问的虚拟机中。

不幸的是,当我关闭远程连接时程序停止执行,给出"OSError: screen grab failed"。

我在网上搜索了解决方案,但我找到的唯一解决方法来自 this link

Use a remote desktop client that allows you to keep outputting the display, even if you minimise the window/close your PC. I used terminals (https://terminals.codeplex.com/ - not affiliated with them at all just recommending as it solved this issue for me).You can set this up to persist the display, and your code should then run fine.

问题是我找不到持久显示的设置。我还发现 this:

pyautogui._pyautogui_x11._display = Xlib.display.Display(
            os.environ['DISPLAY']
        )

...但它似乎也不起作用(给我错误...)。

你有什么解决办法吗?

我已经解决了使用 TightVNC 而不是远程桌面和分离鼠标光标的问题。

根据 here 的回答:

当您与 RDP 断开连接时,Windows 锁定计算机并且不再呈现屏幕,因此任何需要 GUI 的自动化应用程序都无法运行。 要在不丢失屏幕的情况下断开与 RDP 的连接,您可以使用以下命令以管理员身份断开连接(当您需要与 RDP 断开连接时,最好制作一个 bat 文件并 运行 该文件作为管理员):

for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
 %windir%\System32\tscon.exe %%s /dest:console )