我正試圖實現以下功能。我建立了一些gtk應用程序,它會有一些數據,比如說a,b和c。讓vte終端與運行python腳本進行通信
我現在想要的是某種終端窗口,其中我可以查詢和更改數據,就像我在例如IPython的:
$ a
[1 2 3]
$ a= a+1
$ a
[2 3 4]
而且讓這個走在GTK應用效果。這是可行的嗎?
我正試圖實現以下功能。我建立了一些gtk應用程序,它會有一些數據,比如說a,b和c。讓vte終端與運行python腳本進行通信
我現在想要的是某種終端窗口,其中我可以查詢和更改數據,就像我在例如IPython的:
$ a
[1 2 3]
$ a= a+1
$ a
[2 3 4]
而且讓這個走在GTK應用效果。這是可行的嗎?
您可以嘗試通過子進程來啓動的xterm,和file.py和長期之間的溝通,複製環境變量的增值經銷商,並得到它:
os.environ[your_var]
看看這個。一旦你在輸入「python」。關於與腳本進行通信,我發現的唯一方法是使用外部文件。你想要的可能是複雜的。 here你有一個例子,我讓我從VTE終端返回變量「tty」到python腳本。
from gi.repository import Gtk, GObject, Vte
#GObject is not required. I just import it everywhere just in case.
#Gtk, Vte, and GLib are required.
from gi.repository import GLib
import os
#os.environ['HOME'] helps to keep from hard coding the home string.
#os is not required unless you want that functionality.
class TheWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="inherited cell renderer")
self.set_default_size(600, 300)
self.terminal = Vte.Terminal()
self.terminal.fork_command_full(
Vte.PtyFlags.DEFAULT, #default is fine
os.environ['HOME'], #where to start the command?
["/bin/sh"], #where is the emulator?
[], #it's ok to leave this list empty
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
None, #at least None is required
None,
)
#Set up a button to click and run a demo command
self.button = Gtk.Button("Do The Command")
#To get the command to automatically run
#a newline(\n) character is used at the end of the
#command string.
self.command = "echo \"Sending this command to a virtual terminal.\"\n"
command = Gtk.Label("The command: "+self.command)
self.button.connect("clicked", self.InputToTerm)
#end demo command code
#set up the interface
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box.pack_start(self.button, False, True, 0)
box.pack_start(command, False, True, 1)
#a scroll window is required for the terminal
scroller = Gtk.ScrolledWindow()
scroller.set_hexpand(True)
scroller.set_vexpand(True)
scroller.add(self.terminal)
box.pack_start(scroller, False, True, 2)
self.add(box)
def InputToTerm(self, clicker):
#get the command when the button is clicked
length = len(self.command)
#A length is not required but is the easiest mechanism.
#Otherwise the command must be null terminated.
#Feed the command to the terminal.
self.terminal.feed_child(self.command, length)
win = TheWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
你有建立你的vte嗎? – Guillaume
嗨。我不太明白這個問題。我應該如何構建它?但總的來說,我對這些主題的經驗很少,所以可能對您的問題的答案是「否」。 –