6

I am using pyautogui in Windows 10 with Python 3, in a virtual machine accessed by remote desktop.

Unfortunately, the program stops execution when I close remote connection, giving "OSError: screen grab failed".

I've searched for a solution online, but the only workaround that I've found is from 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.

The problem is that I can't find a setting for persisting display. I've also found this:

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

... but it doesn't seems to work either (gives me errors...).

Do you have some workaround to do the trick?

Lore
  • 1,286
  • 1
  • 22
  • 57

2 Answers2

6

Based on an answer from here:

when you disconnect from RDP, Windows lock the computer and does not render the screen any more so any automation apps which needs GUI fails to work. To disconnect from RDP without losing the screen you can disconnect using following commands as administrator (its better to make a bat file and run that file as administrator when you need to disconnect from RDP):

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

I have resolved using TightVNC instead of remote desktop and detaching mouse cursor.

Lore
  • 1,286
  • 1
  • 22
  • 57
  • Can you explain the steps and configuration you used to do this with TigerVNC? Did you have to modify your resolution settings? Or enable auto sign-in on windows? Were you able to schedule job via Windows Task Scheduler or did you have to run them manually after signing in? – emalcolmb Apr 09 '20 at 20:40
  • Hello emalcomb. 1 - I hadn't the necessity. 2 - I have accessed remote with credentials, as far as I remember (was a previous job). 3 - Manually after signing in. – Lore Apr 10 '20 at 09:24
  • The only other particular configuration step was detaching mouse from the options. – Lore Apr 10 '20 at 09:26