[Tkinter-discuss] How to set window's master from HWND on Windows?

Mateusz Loskot mateusz at loskot.net
Thu Oct 10 23:10:45 CEST 2013


Hi,

I'm using Tkinter in embedded Python interpreter on Windows.
There is a desktop Win32 application with a main window opened.
The embedded Python runs a script which creates and
displays a Tkinter window.

Is there any way to assign master (parent) of Tkinter window
based on HWND of a Win32 window?

I'd like to make the Tkinter window a fully modal window
in regard to the main Win32 application window.

I know the winfo_* APIs wiht winfo_id() which allows me to query
Win32 HWND of Tkinter window, but there seems to be no
similar function to set parent by HWND (or id on non-Windows OS).

Is there any Tkinter idiomatic workaround for such problem?

One of ideas I've got is to create a small Python extension, available
from my embedded Python, with exposed custom function to overwrite
HWND of Tkinter windows's parent, using the Win32 SetParent [1].

Here is some sort of a pseudo-code of the idea:

PyObject * mywin32_set_parent(PyObject *self, PyObject *args)
{

  /* unpack Tkinter HWND from args */
  HWND hTkinterWindow = ... ;

  /* returns main application window */
  HWND hAppWindow = GetMyAppWindow();

  /* make the main window parent of the Tkinter */
  ::SetParent(hTkinterWindow, hAppWindow)

  Py_INCREF(Py_None);
  return Py_None;
}

and then in Python script code executed by the embedded Python:

import mywin32
root = Tk()
hwnd  = root.winfo_id()
# make root a child of the main application window overwriting its parent
mywin32.set_parent(hwnd)


I'd appreciate any comments and critique.

[1] http://msdn.microsoft.com/en-us/library/windows/desktop/ms633541.aspx


Best regards,
-- 
Mateusz  Loskot, http://mateusz.loskot.net


More information about the Tkinter-discuss mailing list