Window.activate() not working in Jupyter Notebook
See original GitHub issueWorks 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.
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)
<ipython-input-2-f7654fe7bc43> in <module>()
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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
What to do when things go wrong - The Jupyter Notebook
If you're using a menu shortcut or Anaconda launcher to start it, try opening a terminal or command prompt and running the command...
Read more >can't open jupyter notebook in new conda environment on ...
Activate the new envirionment and then Install jupyter ... Once all the services start to appear running in PowerShell window.
Read more >Working with Jupyter code cells in the Python Interactive window
To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).
Read more >Troubleshooting — Anaconda documentation
(-1708)” when opening a Jupyter notebook on macOS 10.12.5; Missing libgfortran on ... Anaconda 5.0.1 sometimes does not install completely on Windows.
Read more >Using the Jupyter Notebook environment | ArcGIS API for Python
Windows command prompt running jupyter notebook ... root (which is the default), you need to activate that environment before starting the Jupyter Notebook....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not pretty sure why this happens, but I also have a workaround that may look cleaner:
What we do here is try to activate the desired window, then, if this fails, minimize and maximize the same window. No need to import extra libraries or complicated procedures.
Hello guys,
I have encountered the same issue and I’m sorry if my anwser/workaround looks novice but thats what I am so yeah.
Searching for the reason why this happens I fell down the rabbit whole but I think I some what have an anwser but unfortunately I did not find the sollution. Any how what appears to be happening that it does not find a procces/window to foccus on when the funtion is called. Just the program. Once you use the following as an example:
It now seems to bee connected to the procces. after a function like this it seems that the .activate() function seems to work perfectly fine since its pointed to that procces. Until it is minimized because there in no window to focus on.
I have tested it with multiple windows of notepad and looks like it keeps connected to one specific window since “its pointed” to there to bring it to the foreground.
Hopefully I can find the precise reason and get a fully working sollution instead of a workaround
cheers