question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Window.activate() not working in Jupyter Notebook

See original GitHub issue

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

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:open
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

19reactions
khaliuscommented, May 14, 2021

I’m not pretty sure why this happens, but I also have a workaround that may look cleaner:

import pygetwindow as gw
hwnd = gw.getWindowsWithTitle('Notepad')
if hwnd != []:
    try:
        hwnd[0].activate()
    except:
        hwnd[0].minimize()
        hwnd[0].maximize()

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.

5reactions
AllHarmDonecommented, Mar 28, 2020

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:

import pywinauto
import pygetwindow as gw
def focus_to_window(window_title=None):
    window = gw.getWindowsWithTitle(window_title)[0]
    if window.isActive == False:
        pywinauto.application.Application().connect(handle=window._hWnd).top_window().set_focus()

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found