• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
python 列出所有的窗口
#coding=utf-8
 
__author__ = 'Administrator'
 
__doc__ = '''
pythonwin中win32gui的用法
本文件演如何使用win32gui来遍历系统中所有的顶层窗口,
并遍历所有顶层窗口中的子窗口
'''
 
import win32gui
from pprint import pprint
 
def gbk2utf8(s):
    return s.decode('gbk').encode('utf-8')
 
def show_window_attr(hWnd):
    '''
    显示窗口的属性
    :return:
    '''
    if not hWnd:
        return
 
    #中文系统默认title是gb2312的编码
    title = win32gui.GetWindowText(hWnd)
    title = gbk2utf8(title)
    clsname = win32gui.GetClassName(hWnd)
 
    print '窗口句柄:%s ' % (hWnd)
    print '窗口标题:%s' % (title)
    print '窗口类名:%s' % (clsname)
    print ''
 
def show_windows(hWndList):
    for h in hWndList:
        show_window_attr(h)
 
def demo_top_windows():
    '''
    演示如何列出所有的顶级窗口
    :return:
    '''
    hWndList = []
    win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
    show_windows(hWndList)
 
    return hWndList
 
def demo_child_windows(parent):
    '''
    演示如何列出所有的子窗口
    :return:
    '''
    if not parent:
        return
 
    hWndChildList = []
    win32gui.EnumChildWindows(parent, lambda hWnd, param: param.append(hWnd),  hWndChildList)
    show_windows(hWndChildList)
    return hWndChildList
 
 
hWndList = demo_top_windows()
assert len(hWndList)
 
parent = hWndList[20]
#这里系统的窗口好像不能直接遍历,不知道是否是权限的问题
hWndChildList = demo_child_windows(parent)
 
print('-----top windows-----')
pprint(hWndList)
 
print('-----sub windows:from %s------' % (parent))
pprint(hWndChildList)

转载:https://blog.csdn.net/liuyukuan/article/details/52975730

posted on 2018-11-22 13:59  dwtfukgv  阅读(814)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3