如何向pygobject虚拟终端发送命令? [英] How to send commands to pygobject virtual terminal?

查看:33
本文介绍了如何向pygobject虚拟终端发送命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我可以创建一个终端,但输出不用作命令.它只是向虚拟终端打印一个字符串.

Right now I can make a terminal but the output is not used as a command. It just prints a string to the virtual terminal.

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"
")
        v.feed("echo "string"
", length)
        box.pack_start(v, True, True, 0)

        self.add(box)

我尝试使用此处的文档http://developer.gnome.org/vte/0.30/ ,但我遇到了一些麻烦弄清楚这一切.我根本找不到有关 python gtk3 的 vte 的任何文档.

I tried to use the docs here http://developer.gnome.org/vte/0.30/ , but I had some trouble figuring all that out. I couldn't find any documentation on vte for python gtk3 at all.

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

Mainly I'm just trying to figure out how to get the command prompt in the virtual terminal so it will accept commands from inside the python gtk3 interface.

推荐答案

这就是答案.:)重要的部分是 fork_command_full 和 feed_child.

Here's the answer. :) The important parts are fork_command_full and 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(
) character is used at the end of the
        #command string.
        self.command = "echo "Sending this command to a virtual terminal."
"
        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()

这篇关于如何向pygobject虚拟终端发送命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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