0

How to get the HWND of a window made with tkinter?

I have already tried this:

print(root.winfo_id())
print(getAllWindows())

However, there is no root.winfo_id() in the list of getAllWindows() Why so?

Stamend
  • 33
  • 4
  • Out of curiosity, what's an HWND? – Sylvester Kruin Jan 23 '22 at 16:47
  • You can use `pywin32` module in Windows platform: `win32gui.GetParent(root.winfo_id())`. – acw1668 Jan 23 '22 at 16:53
  • Try this link. https://stackoverflow.com/questions/44218662/how-to-get-the-hwnd-of-a-tkinter-window-on-windows – Derek Jan 24 '22 at 03:50
  • Does this answer your question? [How to get the HWND of a Tkinter window on Windows?](https://stackoverflow.com/questions/44218662/how-to-get-the-hwnd-of-a-tkinter-window-on-windows) – Robert Feb 01 '22 at 15:02

1 Answers1

1

This code:

   hwnd = root.winfo_id()
   parent_hwnd = win32gui.GetParent(hwnd)
   text = win32gui.GetWindowText(parent_hwnd)
   print(text)

it will return "System menu", which means it is what you want. Apparently there is a wrapper This simple window coded in PySimpleGUI;

Window coded in pySimpleGUI

have the following windows (as shown by Spy++) :

enter image description here

The first window in the list is the one we catch (it as 'System menu' as title bar!)

digfish
  • 316
  • 2
  • 11