如何使用 Python 消除焦点或最小化窗口?

2022-01-18 00:00:00 python firefox window windows-7

问题描述

我需要将焦点放在指定的窗口上,而我看到的唯一方法是最小化它前面的所有窗口,直到找到正确的窗口...

I need to get focus to a specified window, and the only way I'm seeing on my head, is minimizing all windows on front of it until I get the right one...

我该怎么做?

Windows 7,并没有特定的工具包....

Windows 7, and no specific toolkit....

每种类型的窗口,例如,firefox 和控制台命令

Every type of window, for example, firefox and console command


解决方案

您需要枚举窗口并匹配窗口的标题以获得您想要的.下面的代码搜索标题中带有firefox"的窗口并设置焦点:

You'll need to enumerate through the windows and match the title of the window to get the one you want. The code below searches for a window with "firefox" in the title and sets the focus:

import win32gui

toplist = []
winlist = []
def enum_callback(hwnd, results):
    winlist.append((hwnd, win32gui.GetWindowText(hwnd)))

win32gui.EnumWindows(enum_callback, toplist)
firefox = [(hwnd, title) for hwnd, title in winlist if 'firefox' in title.lower()]
# just grab the first window that matches
firefox = firefox[0]
# use the window handle to set focus
win32gui.SetForegroundWindow(firefox[0])

要最小化窗口,请输入以下行:

To minimize the window, the following line:

import win32con
win32gui.ShowWindow(firefox[0], win32con.SW_MINIMIZE)

相关文章