0

how can I get the handle of a tkinter window? I tried ...

h = self._get_hwnd()

but it doesn't work. I'm using Python IDLE Shell 3.10.1 (OS: Windows 10).

Example:

import tkinter as tk
root=tk.Tk()
root.title=("Test")
text1=tk.Text(root, height=10, width=50)
text1.insert(tk.INSERT,self._get_hwnd())
text1.pack()
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Werner925
  • 39
  • 3
  • 2
    where did you get `self` and what is `__get_hwnd`? your example doesn't work btw and as far as I have tested the methods provided by `tkinter` don't actually give the correct handle (on Windows, but it may work for you) – Matiiss Dec 27 '21 at 20:29
  • 1
    You might be able to find it using one of the techniques shown in answers to the question [Get HWND of each Window?](https://stackoverflow.com/questions/14653168/get-hwnd-of-each-window) (which don't involve using `tkinter`). – martineau Dec 27 '21 at 20:43
  • @Matiiss: If it worked for the OP, they wouldn't be asking the question. – martineau Dec 27 '21 at 20:49
  • Matiiss -I searched in stackoverflow and found self._get_hwnd() (not working). My example works. Try text1.insert(tk.INSERT,"12345678") and tkinter will print 12345678. @martineau I want to find the Handle of tkinter's own (main) window. I'm not searching for foreign windows. – Werner925 Dec 27 '21 at 20:53
  • The code in some of the answers to the linked queation will find the HWND of *any* window, whether it was created by tkinter or not. If you gave your window a unique title, you should be able to find it. – martineau Dec 27 '21 at 20:55
  • @martineau I think there's a python _builtin_ function to return the handle of tkinter's own main window. I'm looking for this function. – Werner925 Dec 27 '21 at 21:01
  • I think you are mistaken about that and was trying to provide you with an alternative. – martineau Dec 27 '21 at 21:03
  • I'm looking for a python _builtin_ function to return the handle of tkinter's current main window – Werner925 Dec 27 '21 at 21:09
  • @martineau I thought that there was a method that would give the window handle (and I just looked and couldn't find it and I know that at one point I tried using some of `tkinter`'s methods to get the handle but they didn't work) – Matiiss Dec 27 '21 at 21:09
  • there is no built-in, there are only a couple of [built-in functions](https://docs.python.org/3/library/functions.html) and AFAIK none of them get window handles, the answer linked by @martineau uses a built-in python module to get window handles – Matiiss Dec 27 '21 at 21:11
  • The code in this question can't work. It doesn't define `self` nor the method `_get_hwnd`. – Bryan Oakley Dec 27 '21 at 21:44
  • @Werner look at my answer [here](https://stackoverflow.com/a/69996847/13629335) you probably want the parent of the window. – Thingamabobs Dec 27 '21 at 21:48

1 Answers1

1

According to the official tk documentation, the winfo_id method will return the HWND on windows.

From the official tcl/tk man page for the winfo command:

winfo id window - Returns a hexadecimal string giving a low-level platform-specific identifier for window. On Unix platforms, this is the X window identifier. Under Windows, this is the Windows HWND. On the Macintosh the value has no meaning outside Tk.

Translated to tkinter, winfo id window is window.winfo_id().

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 3
    The hwnd that is returned by this function isn't the window that OP is expecting. [Look at my answer here](https://stackoverflow.com/a/69996847/13629335) – Thingamabobs Dec 27 '21 at 21:51
  • @Thingamabobs Thanks. I visited your link and it works now: `text1.insert(tk.INSERT,tk.Frame().winfo_id())` – Werner925 Dec 27 '21 at 22:31
  • Werner925: I'm confused, you said you visited Thingamabobs link yet you're using the suggestion in this answer and saying it works now(?) — please clarify how the other answer made it work. – martineau Dec 28 '21 at 22:40