import psutil
import win32gui
import win32process
def enum_window_callback(hwnd, pid):
tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
if pid == current_pid and win32gui.IsWindowVisible(hwnd):
windows.append(hwnd)
for process in psutil.process_iter():
pid = process.pid
windows = []
win32gui.EnumWindows(enum_window_callback, pid)
windownames = [win32gui.GetWindowText(item) for item in windows]
if len(windownames):
exeName = process.name()
for windowName in windownames:
hwnd = win32gui.FindWindow(None, windowName)
print(f'"{exeName}" - "{windowName}" - "{hwnd}"')
пример вывода
"NVIDIA Share.exe" - "NVIDIA GeForce Overlay" - "66250"
"Code.exe" - "list of windows.py - grabscrren - Visual Studio Code" - "459882"
"Code.exe" - "● N-mer-Krammer.py - Рандомный habr - Visual Studio Code" - "132316"
"Battle.net.exe" - "Battle.net" - "132384"
"explorer.exe" - "" - "65860"
"explorer.exe" - "D:\" - "263030"
"explorer.exe" - "" - "65860"
"explorer.exe" - "" - "65860"
"explorer.exe" - "" - "65860"
"explorer.exe" - "" - "65860"
"explorer.exe" - "Program Manager" - "131380"
"python.exe" - "Jupyter Notebook" - "1639536"
"chrome.exe" - "Получение количества открытых окон через WinApi? — Хабр Q&A - Google Chrome" - "1442268"
взято с
https://qna.habr.com/q/1065524 можно наполнить массив открытых окон и если длина массива = 0 принтить что нужно