在 Python GUI 中嵌入 Linux 程序?
我正在为我的汽车构建一个类似于 Tesla Model S 的触摸界面,并且我想要原生支持 Spotify.我正在使用 tkinter 在 Python 中构建我的 GUI,我想知道是否有办法从所述 GUI 的预定义框架内的 GUI 内启动 linux 程序(在本例中为 Spotify).我想我认为这就像网页中的 iframe.
I'm building a touch interface for my car similar to the Tesla Model S and I want to support Spotify natively. I'm building my GUI in Python using tkinter and I'm wondering if there is a way to launch a linux program (in this case Spotify) from within a GUI within a pre-defined frame of said GUI. I guess I'm thinking of this like an iframe in a webpage.
我知道这可能不是最好的方法,但我的车 99% 的时间都处于离线状态,所以我需要支持离线流媒体,我可以从 Spotify 应用程序中做到这一点,而不是经常使用他们的网络 API.
I know this probably isn't the best way to do it, but my car will be offline 99% of the time, so I need to support offline streaming, which I can do from the Spotify application and not so much using their web API.
Tkinter 能够嵌入其他基于 X11 的应用程序,但前提是 Windows 支持嵌入自身.
Tkinter has the ability to embed other X11-based applications, but only if the windows supports embedding itself.
诀窍是获取 tkinter 小部件的 X 窗口 ID,然后让其他程序写入该窗口 ID.
The trick is to get the X window id of a tkinter widget, and then letting the other program write to that window id.
据我所知,只有少数程序可以实现这一点.xterm 就是其中之一.我想我过去也用过 mplayer.
As far as I know, there's only a handful of programs that make this possible. xterm is one. I think I've used mplayer in the past as well.
这是一个使用 xterm 的非常简单的例子:
Here's a very simple example using xterm:
import tkinter as tk
import subprocess
root = tk.Tk()
root.geometry("400x400")
label = tk.Label(root, text="Example of xterm embedded in frame")
xterm_frame = tk.Frame(root)
label.pack(side="top", fill="x")
xterm_frame.pack(fill="both", expand=True, padx=20, pady=20)
xterm_frame_id = xterm_frame.winfo_id()
subprocess.call("xterm -into %d &" % xterm_frame_id, shell=True)
root.mainloop()