Python implements the method of traversing all Windows and outputting window titles

  • 2020-04-02 14:39:36
  • OfStack

This article illustrates the Python implementation of traversing all Windows and output window titles. Share with you for your reference. The details are as follows:

This code lets Python walk through all the Windows running the program under the current Windows and get the title output of the running window


#! /usr/bin/env python
# -*- coding: utf-8 -*-
from win32gui import *
titles = set()
def foo(hwnd,mouse):
 # Remove the following sentence and you have all the output, but I don't need that much 
 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

To output Chinese, change the last sentence to:


print(t.decode('GB2312'))

Transcode GB2312 into Unicode output so that the output window title is normal Chinese.

I hope this article has helped you with your Python programming.


Related articles: