首页  >  文章  >  后端开发  >  如何在 Python 中以编程方式检索活动窗口?

如何在 Python 中以编程方式检索活动窗口?

Patricia Arquette
Patricia Arquette原创
2024-10-21 16:11:02863浏览

How to Programmatically Retrieve the Active Window in Python?

在 Python 中获取活动窗口

问题:如何使用 Python 访问屏幕上的活动窗口?例如,此窗口可以是路由器的管理登录界面。

解决方案:

在 Windows 上,您可以使用 Python for Windows 扩展(pywin32)来获取活动窗口。操作方法如下:

<code class="python"># Python 2
from win32gui import GetWindowText, GetForegroundWindow
print GetWindowText(GetForegroundWindow())</code>
<code class="python"># Python 3
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))</code>

示例:

下面的代码在屏幕上打印活动窗口的标题:

<code class="python">import win32gui

window_handle = win32gui.GetForegroundWindow()
window_title = win32gui.GetWindowText(window_handle)
print(window_title)</code>

以上是如何在 Python 中以编程方式检索活动窗口?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn