2012-08-03 42 views
0

現在我可以製作一個終端,但輸出不被用作命令。 它只是打印一個字符串到虛擬終端。如何發送命令到pygobject虛擬終端?

from gi.repository import Gtk, GObject, Vte 

class TheWindow(Gtk.Window): 

    def __init__(self): 
     Gtk.Window.__init__(self, title="inherited cell renderer") 
     self.set_default_size(400, 200) 

     box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) 
     v = Vte.Terminal() 
     #v.connect ("child-exited", lambda term: gtk.main_quit()) 
     length = len("echo \"string\"\n") 
     v.feed("echo \"string\"\n", length) 
     box.pack_start(v, True, True, 0) 

     self.add(box) 

我想在這裏使用的文檔 http://developer.gnome.org/vte/0.30/,但我遇到了一些麻煩搞清楚這一切,。我根本找不到任何關於python gtk3的文檔。

主要我只是想弄清楚如何在虛擬終端中獲得命令提示符,以便它接受來自python gtk3接口的命令。

+0

下面是關於它的http://askubuntu.com/questions/154354/how-to-add-vte-terminal-widget-in-gtk3一些更多的信息下面我能寫命令這些指令後在終端直接。現在我需要弄清楚如何從gtk中完成它。 – 2012-08-04 10:46:44

回答

5

答案如下。 :) 重要的部分是fork_command_full和feed_child。

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

該腳本效果很好,但是你知道是否有任何方法可以避免打印命令?例如:'echo「將此命令發送到虛擬終端。」'? – rsm 2014-08-10 16:42:10

+0

我不確定你的意思。如果您想運行未打印的命令,則不需要虛擬終端。 Popen會做到這一點。 – 2014-08-11 01:01:05

+0

(我在問如何只打印vte中的命令輸出,它對實時打印非常有用),但我只是覺得它如何去做。在VTE終端中寫'tty'來獲得'/ dev/pts /#',然後通過執行'echo'foo'>/dev/pts /#'來重定向其他進程的bash命令。 – rsm 2014-08-11 01:57:13