[Python] Error: module 'win32process' has no attribute 'GetWindowThreadProcessId'

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    as the title says, python 3.10 doesn't find GetWindowThreadProcessId() in the win32process library. I've taken a while to search for this error but it seems to work for everyone else.
    I am running my code within a python 3.10 venv created by pycharm. I installed the Libraries today from within the venv.

    My program is the following (it tries to get some details of the current foreground window):

    import win32gui
    import win32process
    import psutil
    import time
    from datetime import datetime

    last_window = None

    while True:
    try:
    hwnd = win32gui.GetForegroundWindow()
    if hwnd != last_window:
    last_window = hwnd

    # here is the problem, this doesn't deem to exist for my python
    tid, pid = win32process.GetWindowThreadProcessId(hwnd)
    proc = psutil.Process(pid)
    window_title = win32gui.GetWindowText(hwnd)
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{timestamp}] Focus switched to PID {pid} ({proc.name()}) - '{window_title}'")
    except Exception as e:
    print(f"Error: {e}")
    time.sleep(0.005) # checks every 50ms

    Continue reading...

Compartilhe esta Página