class WindowManager:

    def __init__(self):
        self._handle = None

def _window_enum_callback(self, hwnd, wildcard):
    if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:
        self._handle = hwnd

def find_window_wildcard(self, wildcard):
    self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)

def set_foreground(self):
    win32gui.ShowWindow(self._handle, win32con.SW_RESTORE)
win32gui.SetWindowPos(self._handle, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, 
    win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
win32gui.SetWindowPos(self._handle, win32con.HWND_TOPMOST, 0, 0, 0, 0, 
    win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
win32gui.SetWindowPos(self._handle, win32con.HWND_NOTOPMOST, 0, 0, 0, 0, 
    win32con.SWP_SHOWWINDOW + win32con.SWP_NOMOVE + win32con.SWP_NOSIZE)
shell = win32com.client.Dispatch('WScript.Shell')
shell.SendKeys('%')
win32gui.SetForegroundWindow(self._handle)

def find_and_set(self, search):
    self.find_window_wildcard(search)
self.set_foreground()
Keeping the terminal in focus