栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python窗口激活

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python窗口激活

您可以使用win32gui模块来执行此操作。首先,您需要在窗口上获得有效的句柄。

win32gui.FindWindow
如果您知道窗口类名称或确切名称,则可以使用。如果没有,您可以使用枚举窗口,
win32gui.EnumWindows
然后尝试找到合适的窗口。

拥有手柄后,即可

win32gui.SetForegroundWindow
使用手柄调用。它将激活该窗口,并准备好进行击键。

请参见下面的示例。希望对您有所帮助

import win32guiimport reclass WindowMgr:    """Encapsulates some calls to the winapi for window management"""    def __init__ (self):        """Constructor"""        self._handle = None    def find_window(self, class_name, window_name=None):        """find a window by its class_name"""        self._handle = win32gui.FindWindow(class_name, window_name)    def _window_enum_callback(self, hwnd, wildcard):        """Pass to win32gui.EnumWindows() to check all the opened windows"""        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None: self._handle = hwnd    def find_window_wildcard(self, wildcard):        """find a window whose title matches the wildcard regex"""        self._handle = None        win32gui.EnumWindows(self._window_enum_callback, wildcard)    def set_foreground(self):        """put the window in the foreground"""        win32gui.SetForegroundWindow(self._handle)w = WindowMgr()w.find_window_wildcard(".*Hello.*")w.set_foreground()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646123.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号