以下脚本应该可以在Linux、Windows和Mac上运行。它目前只在Linux上测试(UbuntuMateUbuntu15.10)。
先决条件
对于Linux:
在Ubuntu上安装wnck(sudo apt-get install python-wnck,请参阅libwnck)
对于Windows:
确保win32gui可用
对于Mac:
确保AppKit可用
剧本#!/usr/bin/env python
"""Find the currently active window."""
import logging
import sys
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG,
stream=sys.stdout)
def get_active_window():
"""
Get the currently active window.
Returns
-------
string :
Name of the currently active window.
"""
import sys
active_window_name = None
if sys.platform in ['linux', 'linux2']:
# Alternatives: http://unix.