我编写了以下代码,但我发现EnumWindows()枚举了屏幕上所有的顶级窗口。我希望我可以通过PID获得所有过程窗口。我该怎么办?
import win32gui
import win32process
def get_hwnds(pid):
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
thread_id, found_pid = win32process.GetWindowThreadProcessId(hwnd)
if found_pid == pid:
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
return hwnds
hwnds = get_hwnds(123456)