Python实现遍历windows所有窗口并输出窗口标题的方法

626次阅读  |  发布于5年以前

本文实例讲述了Python实现遍历windows所有窗口并输出窗口标题的方法。分享给大家供大家参考。具体如下:

这段代码可以让Python遍历当前Windows下所有运行程序的窗口,并获得运行窗口的标题输出


    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    from win32gui import *
    titles = set()
    def foo(hwnd,mouse):
     #去掉下面这句就所有都输出了,但是我不需要那么多
     if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
      titles.add(GetWindowText(hwnd))
    EnumWindows(foo, 0)
    lt = [t for t in titles if t]
    lt.sort()
    for t in lt:
     print t

若要输出中文,可以将最后一句改成:


    print(t.decode('GB2312'))

将GB2312转码成Unicode输出,这样输出的窗口标题就是正常的中文。

希望本文所述对大家的Python程序设计有所帮助。

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8