14

I get the active window like so:

window = win32gui.GetForegroundWindow()

which is an Int, say 1053634. And afterwards I try to set the foreground window back to the specified window:

win32gui.SetForegroundWindow(window)

And I get this error: win32gui.SetForegroundWindow(window) error: (127, 'SetForegroundWindow', 'The specified procedure could not be found.')

Sometimes when I do this in the interpreter, I get this error:

win32gui.SetForegroundWindow(1053634)
error: (0, 'SetForegroundWindow', 'No error message is available')

What do you think is the problem?

Thanks!

Wise
  • 628
  • 2
  • 11
  • 25

3 Answers3

32

My program works fine on my desktop with Windows 7, but when I use my laptop with Windows Vista (even with UAC off), I get the error:

pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')

The program will flash in the taskbar, but no characters are sent.

I even tried sending 'notepad' SetForegroundWindow and get the same error.

Here is a link with a workaround that combines threads to get the computer to think they work together: http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/

This article has more information about the problem.

UPDATE: I'm sorry that link goes to a C program. I researched some more and found out it will let you SetForegroundWindow, if you send an alt key first.

For example:

import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(window)
Jeff B
  • 8,572
  • 17
  • 61
  • 140
timmyt123
  • 401
  • 5
  • 8
  • Could you please clarify what is "WScript.Shell" if we have already opened an application? – Eduard Grigoryev Jul 15 '20 at 20:06
  • got Error When call in background: pywintypes.com_error: (-2147221008, '尚未调用 CoInitialize。', None, None) – CS QGB Jun 16 '21 at 12:24
  • The block of code works well for me. But may I ask what exactly `win32com.client.Dispatch("WScript.Shell").SendKeys('%')` does? Also, if I call `win32gui.SetForegroundWindow()` multi times in one Python script, do I need to `SendKeys` every times, or just send once in the beginning will be fine? Btw, the link to the article seems not working now. – Hui Gordon Jan 10 '22 at 11:28
  • 1
    @HuiGordon Literally all it does is sending left alt key. For some reason windows really wants to have you to press alt before foregrounding any other window. Not sure if this is a bug or intended by microsoft maybe they want people to only be able to do Alt + Tab or something to bring windows to the front. – Mika C. May 12 '22 at 21:12
1

What worked it me...

import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
input("Press Enter")
shell.SendKeys(' ') #Undocks my focus from Python IDLE
win32gui.SetForegroundWindow(window) #It works!
shell.SendKeys('%')
VectorASD
  • 11
  • 2
0

The problem is you can't set foreground if the window is minimized so I offer my poor man's improvement to the existing answers:

import win32gui
import win32con

win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
win32gui.SetForegroundWindow(hwnd)
Kelvin Wang
  • 627
  • 5
  • 21