asweigart / asweigart/PyGetWindow

Window.activate() not working in Jupyter Notebook

Open
#16 12 comments 2 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
422
Forks
73
PR merge metrics
No merged PRs in 30d

Description

Works fine in a `cmd` but when I run it on a Jupyter Notebook (Colab local runtime):
```
import pyautogui
pyautogui.getWindowsWithTitle('Code')[0].activate()
```

VSCode flashes on the taskbar but never activated.
![image](https://user-images.githubusercontent.com/12462089/65833513-61fab600-e2fb-11e9-8c7f-6042191b5760.png)

Strangely if I focus the window first with `pywinauto`:
```
import pywinauto
pywinauto.application.Application().connect(best_match='Code').top_window().set_focus()
```
then it would work normally, until I restarted the notebook. This only affects one window, other windows have to be focused with `pywinauto` before being able to be activated with `pygetwindow`.

Kernel output:
```
---------------------------------------------------------------------------
PyGetWindowException Traceback (most recent call last)
in ()
1
2 import pyautogui
----> 3 pyautogui.getWindowsWithTitle('Code')[0].activate()

c:\users\dleanjeans\appdata\local\programs\python\python36\lib\site-packages\pygetwindow\_pygetwindow_win.py in activate(self)
282 result = ctypes.windll.user32.SetForegroundWindow(self._hWnd)
283 if result == 0:
--> 284 _raiseWithLastError()
285
286

c:\users\dleanjeans\appdata\local\programs\python\python36\lib\site-packages\pygetwindow\_pygetwindow_win.py in _raiseWithLastError()
95 information from GetLastError() and FormatMessage()."""
96 errorCode = ctypes.windll.kernel32.GetLastError()
---> 97 raise pygetwindow.PyGetWindowException('Error code from Windows: %s - %s' % (errorCode, _formatMessage(errorCode)))
98
99

PyGetWindowException: Error code from Windows: 0 - The operation completed successfully.
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.