Werner925
Werner925

Reputation: 39

Getting tkinter current window handle

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()

Upvotes: 0

Views: 1466

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

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().

Upvotes: 2

Related Questions