1

问题陈述:在 GUI 中嵌入一个用于 linux 的终端仿真器,并通过 GUI 组件向它提供自定义命令。

使用:

蟒蛇 3

gtk+3

vte.get_minor_version(): 58

vte.get_major_version(): 0

vte.get_macro_version(): 2

我一直在尝试在 PyGtk 应用程序中嵌入终端仿真器(如 PyCharm 和 VScode 中的那些),并通过 Gtk GUI 为其提供系统命令。我尝试过 Vte 在按下按钮时使用 Terminal.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)
        global terminal
        terminal     = Vte.Terminal()
        terminal.spawn_sync(
                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(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.
        # terminal.feed_child(self.command, length )
        # terminal.feed_child(self.command)
        # command = "hello"
        terminal.feed_child(self.command)
        print(Vte.get_minor_version())
        os.system("suhelp")


win = TheWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()```

this results in following error:
```File "......", line 59, in InputToTerm
    terminal.feed_child(self.command)
TypeError: Item 0: Must be number, not str

有人可以帮我解决这个问题吗?

有没有解决问题陈述的替代方法?

4

3 回答 3

2

Vte.Terminal.feed_child()在 Python 中如何生成它似乎有一个错误。如果您将说明参数 is的Python 文档C 文档进行比较,那么您可以看到差异。intNone

我建议通过使用feed_child_binary()一个bytes对象来解决这个问题,并将错误报告给 PyGObject。

于 2020-02-29T07:33:41.570 回答
0

只为寻找的人。利用

...
def feed_input(self, text):
   text = bytesarray(text, "utf-8")
   self.terminal.feed(text)
...
于 2020-10-27T16:52:46.667 回答
-1

这是终端仿真器类,可以像盒子一样打包到窗口中。它还具有上下文菜单弹出功能

class termaxx():
def __init__ (self):
    self.main_box = Gtk.HBox()
    self.terminal_box = Gtk.HBox()
    self.terminal     = Vte.Terminal()
    self.terminal.spawn_sync(Vte.PtyFlags.DEFAULT, os.getcwd(), 
                ["/bin/bash"], [], GLib.SpawnFlags.DO_NOT_REAP_CHILD, None, None,)
    self.terminal_box.pack_start(self.terminal, True, True, 0)
    self.scrollbar = Gtk.Scrollbar(orientation=Gtk.Orientation.VERTICAL,
                    adjustment = Gtk.Scrollable.get_vadjustment(self.terminal))
    self.terminal_box.pack_start(self.scrollbar,False, False, 0)
    self.main_box.pack_start(self.terminal_box,True,True,0)
    self.main_box.show_all()
    self.startup_cmds=["PS1='Termaxx@$PWD $: '\n","clear\n"]

    self.menu = Gtk.Menu()

    self.menuitem1 = Gtk.MenuItem.new_with_label("Copy")
    self.menuitem1.connect("activate",self.copy)
    self.menuitem1.show()

    self.menuitem2 = Gtk.MenuItem.new_with_label("Paste")
    self.menuitem2.connect("activate",self.paste)
    self.menuitem2.show()

    self.menuitem3 = Gtk.MenuItem.new_with_label("Clear")
    self.menuitem3.connect("activate",self.clear)
    self.menuitem3.show()

    self.menu.append(self.menuitem1)
    self.menu.append(self.menuitem2)
    self.menu.append(self.menuitem3)
    

    for i in self.startup_cmds:
        self.run_command(i)
    self.main_box.connect_object("event", self.button_press, self.menu)

def run_command(self,cmd):
    self.terminal.feed_child_binary(bytes(cmd,'utf8'))

def button_press(self, widget, event):
    if event.type == Gdk.EventType.BUTTON_RELEASE:
        x,button = event.get_button()
        if button == Gdk.BUTTON_SECONDARY:
            widget.popup(None,None, None, None, button, Gdk.CURRENT_TIME) 

def copy(self,widget):
    self.terminal.copy_primary()

def paste(self,widget):
    self.terminal.paste_primary()

def clear(self,widget):
    self.run_command("clear\n")                 
于 2020-05-18T16:40:15.370 回答