简体   繁体   English

使用python在单个窗口中打开其他应用程序

[英]Opening other application in single window using python

I am new to python and want to learn this from the basic. 我是python的新手,想从基础上学习它。 I want to open two other application like notepad in a single window. 我想在一个窗口中打开其他两个应用程序,例如记事本。 I searched for any examples by using "tkinter". 我使用“ tkinter”搜索了所有示例。 Any idea for implementing this using "tkinter". 使用“ tkinter”实现此目标的任何想法。

PS: two tabs one in left and one in right so that one tab will be having notepad and other will have a media player (let say) and i should be able to close the single parent window for closing this child windows. PS:两个选项卡一个在左边,一个在右边,这样一个选项卡将具有记事本,另一个选项卡将具有媒体播放器(可以说),我应该能够关闭单个父窗口以关闭此子窗口。

For what you need, you will have to use libraries like tkinter or pyqt. 对于您所需要的,您将不得不使用tkinter或pyqt之类的库。 As you want to learn it from the basic, I would suggest you to pick a GUI library and start learning it. 当您想从基础上学习它时,建议您选择一个GUI库并开始学习它。 Eventually you will know how to get the result that you desire. 最终,您将知道如何获得所需的结果。

I don't think you will be able to do this easily. 我认为您无法轻松做到这一点。 Notepad is using its own toolkit which, most probably, is not compatible with the one you are using. 记事本正在使用自己的工具包,很可能与您使用的工具包不兼容。

If you want to open eg. 如果你想打开例如。 an editor, look for a widget in the toolkit of your choice which does what you want (or nearly). 编辑器,然后在您选择的工具包中寻找可以完成您想要的(或几乎要完成)工作的小部件。 Eg. 例如。 if you want an editor, use the GtkTextView in GTK, or, if you want something that can do code editing, look at GtkSourceView, etc. Same goes for media applications. 如果需要编辑器,请在GTK中使用GtkTextView,或者,如果需要可以进行代码编辑的内容,请查看GtkSourceView等。媒体应用程序也是如此。

The 'include' applications as part of a project works only to a certain point. 作为项目一部分的“包含”应用程序仅在一定程度上起作用。 You can eg, include text-mode applications in a terminal window, but graphical apps are much harder. 例如,您可以在终端窗口中包含文本模式应用程序,但是图形应用程序要困难得多。

On Unix systems with X server the XEmbed protocol exists for such purpose, but it requires the child application to be specifically prepared for that (here's a related question ). 在具有X服务器的Unix系统上,存在XEmbed协议用于此目的,但是它需要为此专门准备子应用程序(这是一个相关的问题 )。

On Windows this can be accomplished using the Win32 API. 在Windows上,可以使用Win32 API来完成。 You have to get the handles of the parent and child windows and use the SetParent function. 您必须获取父窗口和子窗口的句柄并使用SetParent函数。

Here's a proof of concept: 这是概念证明:

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

Keep in mind that you have to install PyWin32 for this to work. 请记住,您必须安装PyWin32才能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM