E
E
Ermac3012021-08-27 08:34:56
Python
Ermac301, 2021-08-27 08:34:56

How to bring a window to the front?

I need a concept of how to bring a window in Windows with a certain name to the fore.
A specific, working version was stupidly not found on the Internet, either the plugins are dead, or the commands simply do not work.
For example, I need to switch to a window whose name contains "notepad", what script is needed in order to bring this window to the foreground?

This does not work:

hwnd = win32gui.FindWindow(None,"notepad")
win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
win32gui.SetForegroundWindow(hwnd)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2021-08-27
@Ermac301

Working code. Another thing is if there is no window with the name "notepad".
You can iterate through all windows and find those that have "notepad"

def enum_handler(hwnd, ctx):
    if win32gui.IsWindowVisible(hwnd):
        if 'notepad' in win32gui.GetWindowText(hwnd):
            win32gui.SetForegroundWindow(hwnd)

win32gui.EnumWindows(enum_handler, None)

E
Ermac301, 2021-09-01
@Ermac301

I found these commands, but they open the file:

#Найти и запустить приложение (программу)
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe")

This is not quite what I need, I need to bring an already open window to the foreground.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question