5

How to get the HWND of a Tkinter window in python3 on windows?

  • I would like to get the native window handle of the Tkinter window if possible.
  • I would need either the HWND or HDC of the window to perform custom drawing operations.

Is there a way to get the HWND of the Canvas or any other child components?

There is a way to list all the windows created by the current process. I am also able to iterate through the child windows of any window listed previously. This would be the ugly solution for the problem.

Please post any alternative solutions if Tkinter does not expose the handle directly.

martineau
  • 119,623
  • 25
  • 170
  • 301
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71

1 Answers1

7

I believe what you need is the TopLevel window method.

Check out this link

frame() [#]
Returns a string containing a system-specific window identifier corresponding to the window’s outermost parent. For Unix, this is the X window identifier. For Windows, this is the HWND cast to a long integer.

There is also additional information about getting the HWND here.

winfo_id() [#]
Get a system-specific window identifier for this widget. For Unix, this is the X window identifier. For Windows, this is the HWND cast to a long integer.

    Returns:
      The window identifier.

I believe using winfo_id() above, you should be able to get the HWND of the window.

martineau
  • 119,623
  • 25
  • 170
  • 301
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79