【问题标题】:How to send commands to pygobject virtual terminal?如何向 pygobject 虚拟终端发送命令?
【发布时间】:2013-04-30 04:16:01
【问题描述】:

现在我可以制作一个终端,但输出不用作命令。 它只是将一个字符串打印到虚拟终端。

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/ ,但我在弄清楚这一切时遇到了一些麻烦。我根本找不到任何关于 vte for python gtk3 的文档。

主要是我只是想弄清楚如何在虚拟终端中获取命令提示符,以便它接受来自 python gtk3 接口内部的命令。

【问题讨论】:

  • 这里有更多关于它的信息askubuntu.com/questions/154354/… 按照这些说明,我可以直接在终端中编写命令。现在我需要从 gtk 弄清楚如何做到这一点。

标签: python terminal gtk gtk3 pygobject


【解决方案1】:

这就是答案。 :) 重要的部分是 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()

【讨论】:

  • 脚本运行良好,但您知道是否有任何方法可以避免打印命令吗?例如:echo "Sending this command to a virtual terminal." ?
  • 我不确定你的意思。如果要运行未打印的命令,则不需要虚拟终端。 Popen 会这样做。
  • (我在问如何只在 vte 中打印命令的输出,这对于实时打印很有用)但我只是找到了它的方法。在 VTE 终端中写入 tty 在 other 中获取 /dev/pts/# 然后可以通过执行 echo 'foo' > /dev/pts/# 将来自其他进程的 bash 命令重定向到那里
  • 如果包含非 ascii 字符,该命令将不会被打印。所以,在一行中:self.terminal.feed_child(self.command, len(self.command.decode()))
  • 函数“fork_command_full”已重命名为“spawn_sync”并已弃用。
猜你喜欢
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多