如何使用 Python 消除焦点或最小化窗口? [英] How do I take out the focus or minimize a window with Python?

查看:31
本文介绍了如何使用 Python 消除焦点或最小化窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

这篇关于如何使用 Python 消除焦点或最小化窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆