本文實例講述了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程序設計有所幫助。