Python 获得特定的 window 标题

Python get specific window title

我希望能够检测到特定命名的 window(在任何进程中)的存在,以用作 "if" 变量

经过一些研究,我得出结论我必须使用 win32gui 库(一些帖子也建议 ctypes),但我找到的大多数帖子都是关于 making a list of processes, and the only one that was related to this topic didn't answer the question properly. Perhaps I am too novice to understand it, but at last, I couldn't get it to work (the question was this one 的)

为了举例说明我想要什么,我将分享一个简单的 .vbs 代码来执行此操作:它搜索特定名称(在我们的例子中是 steve jobs),如果它在任何地方找到它,它就会触发(在这个例子中,它关闭了所述进程)

do
WindowTitle = "steve jobs"
set shell = createObject("wscript.shell") 
success = shell.appactivate(WindowTitle)
if success then shell.sendkeys "%{F4}" 
loop
import win32gui

win2find = input('enter name of window to find')
whnd = win32gui.FindWindowEx(None, None, None, win2find)
if not (whnd == 0):
  print('FOUND!')

您可以在 Google 中搜索 'FindWindowEx'

找到https://msdn.microsoft.com/en-us/library/windows/desktop/ms633500(v=vs.85).aspx

有关 FindWindowEx 函数的说明