def _set_foreground_window(hwnd): shell = win32com.client.Dispatch('WScript.Shell') shell.SendKeys('%') win32gui.SetForegroundWindow(hwnd)