Home > Backend Development > Python Tutorial > Python实现遍历windows所有窗口并输出窗口标题的方法

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

WBOY
Release: 2016-06-06 11:22:49
Original
1708 people have browsed it

本文实例讲述了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
Copy after login

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

print(t.decode('GB2312'))
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template