Open ghost opened 3 years ago
Hi, I have the same exact error.
Hi I have the same error. The first time call win.activate() is working. But get the error when call the second time with other window. Could you take a look please? Thanks!
Same here. The problem is caused by ctypes.windll.user32.SetForegroundWindow returning False / 0 for any hWnd but the one of the active python programm.
Hi, I got the same issue. As a workaround I use
window.minimize()
window.restore()
I learned there are limitations when working with this method. When I tried the .activate()
method in a terminal (REPL mode), it threw pygetwindow.PyGetWindowException: Error code from Windows: 6 - The handle is invalid.
and the target program had a red flash in the taskbar.
Reading the SetForegroundWindow doc:
An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.
So I tried invoking .activate()
in an automated python script, it worked as expected.
It seems there is a short-term solution available. Reference: https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found/15503675#15503675
The above web page is an example of win32gui, but it appears to be applicable to this library as well.
Specifically, the issue can be resolved by inputting the Alt
key right before calling activate()
.
code example:
import pyautogui
# some process...
if not window.isActive:
pyautogui.press('altleft')
window.activate()
It seems there is a short-term solution available. Reference: https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found/15503675#15503675
The above web page is an example of win32gui, but it appears to be applicable to this library as well. Specifically, the issue can be resolved by inputting the
Alt
key right before callingactivate()
.code example:
import pyautogui # some process... if not window.isActive: pyautogui.press('altleft') window.activate()
Wow, it works!
Same after using it regularly for months; here's the error where DeltekWindow is from DeltekWindow = gw.getWindowsWithTitle('Deltek')[0]
Traceback (most recent call last): File "C:\Users\aaw\AppData\Local\Programs\Python\Python38-32\pyTimesheet.py", line 51, in
DeltekWindow.activate()
File "C:\Users\aaw\AppData\Roaming\Python\Python38\site-packages\pygetwindow_pygetwindow_win.py", line 237, in activate
_raiseWithLastError()
File "C:\Users\aaw\AppData\Roaming\Python\Python38\site-packages\pygetwindow_pygetwindow_win.py", line 97, in _raiseWithLastError
raise PyGetWindowException('Error code from Windows: %s - %s' % (errorCode, _formatMessage(errorCode)))
pygetwindow.PyGetWindowException: Error code from Windows: 0 - The operation completed successfully.