在带有X服务器的Unix系统上,XEmbed协议就是为了这个目的而存在的,但是它要求子应用程序为此专门准备(这里有一个相关的question)。在
在Windows上,这可以用win32api来实现。您必须获得父窗口和子窗口的句柄并使用SetParent函数。在
以下是概念证明:from tkinter import Tk
from win32gui import SetParent, FindWindow, SetWindowPos
import time
import subprocess
def attach_window(window_class, parent, x, y, width, height):
child = FindWindow(window_class, None)
SetParent(child, parent)
SetWindowPos(child, 0, x, y, width, height, 0)
def main():
root = Tk()
subprocess.Popen('C:\\Windows\\system32\\notepad.exe')
subprocess.Popen('C:\\Windows\\system32\\calc.exe')
# Give child processes enough time to launch
time.sleep(0.5)
# Get the HWND of the parent window
parent = int(root.frame(), 16)
attach_window('Notepad', parent, 0, 0, 400, 200)
attach_window('CalcFrame', parent, 0, 205, 420, 320)
root.geometry('500x500')
root.mainloop()
if __name__ == '__main__':
main()
请记住,您必须安装PyWin32才能使其工作。在