asweigart / PyGetWindow

A simple, cross-platform module for obtaining GUI information on applications' windows.
BSD 3-Clause "New" or "Revised" License
362 stars 69 forks source link

Activate does not work #36

Open ghost opened 3 years ago

widb0005 commented 3 years ago

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.

William-Bourget commented 3 years ago

Hi, I have the same exact error.

saintyoung commented 3 years ago

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!

noacharon commented 3 years ago

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.

s-weigand commented 3 years ago

Hi, I got the same issue. As a workaround I use

window.minimize()
window.restore()
medifle commented 1 year ago

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.

eito-88 commented 8 months ago

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()
ShiqiRao commented 4 months ago

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()

Wow, it works!