在 Python GUI 中嵌入 Linux 程序? [英] Embed Linux Program in Python GUI?

查看:34
本文介绍了在 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()

这篇关于在 Python GUI 中嵌入 Linux 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆