[英]Making Tkinter window a child of non-Tkinter window
(This question was originally posted to python-tkinter mailing list) (这个问题最初发布在python-tkinter邮件列表中)
TL;TR How to set Tkinter window's master from HWND on Windows? TL; TR如何在Windows上从HWND设置Tkinter窗口的主设备?
I'm using Tkinter in embedded Python interpreter on Windows. 我在Windows上使用嵌入式Python解释器中的Tkinter。 There is a desktop Win32 application with a main window opened. 有一个桌面Win32应用程序打开了一个主窗口。 The embedded Python runs a script which creates and displays a Tkinter window. 嵌入式Python运行一个脚本,用于创建和显示Tkinter窗口。
Is there any way to assign master (parent) of Tkinter window based on HWND
of a Win32 window? 有没有办法根据Win32窗口的HWND
分配Tkinter窗口的主(父)?
I'd like to make the Tkinter window a fully modal window in regard to the main Win32 application window. 我想使Tkinter窗口成为主Win32应用程序窗口的完全模态窗口。
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). 我知道winfo_*
API与winfo_id()
允许我查询Tkinter窗口的Win32 HWND
,但似乎没有类似的功能来设置HWND
(或非Windows操作系统上的id)的父级。
One of rought 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
. 我得到的一个想法是创建一个小的Python扩展,可以从我的嵌入式Python中获得,使用Win32 SetParent
覆盖Tkinter窗口父级的HWND
的外接自定义函数。
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: 然后在嵌入式Python执行的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)
To simplify, let's assume the only purpose of setting master from HWND
is to position the Tkinter window relatively (or on top) to the window which HWND
is assigned as master. 为了简化,我们假设从HWND
设置主设备的唯一目的是将Tkinter窗口相对(或在顶部)定位到HWND
被指定为主设备的窗口。 IOW, the Tkniter window would behave like a modal dialog of non-Tkinter window. IOW,Tkniter窗口的行为类似于非Tkinter窗口的模态对话框。
Is there any Tkinter idiomatic way to achive such behaviour? 是否有任何Tkinter惯用的方式来实现这种行为?
Try with buttons: 试试按钮:
from tkinter import *
def Test():
Test = Toplevel()
main = Tk()
main.title("<Your title>")
main.geometry("400x400")
Button(main, text="<Your text>", command=Test).grid(row=0, column=0, sticky=W)
main.mainloop()
That's what we get: See the picture 这就是我们得到的: 见图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.