How to get a string with the name of the window that is focused in python?

Asked

Viewed 387 times

4

Some time ago I started using pyHook to manipulate events. However, the original version has errors, so I switched it to an alternative version that corrected the errors of the original version. However, I found that this alternate version always shows an empty string for the method that returns the name of the window that is focused.

from pyHook import HookManager
from python import PumpMessages

def evento_teclado(event):
    print(event.WindowName())
    return True

hm=HookManager()
hm.KeyDown=evento_teclado
hm.HookKeyboard()
PumpMessages()

This script always shows an empty string. Is there any other way to get the window name?

  • What OS? If it’s Windows, have you tried? import win32gui
 win32gui.GetWindowText (w.GetForegroundWindow())?

  • It’s Windows. And it worked. It puts the answer I accept. @leonardopessoa

1 answer

4


Try to use the module win32gui:

import win32gui window = win32gui.GetWindowText(w.GetForegroundWindow())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.