如何在像VScode和PyCharm的PyGtk应用中实现Linux终端?

问题描述 投票:1回答:1

问题陈述:在GUI中嵌入Linux终端仿真器,并通过GUI组件向其提供自定义命令。

使用:

Python 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

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

是否有其他方法可以解决问题陈述?

python gtk terminal-emulator vte
1个回答
0
投票

Vte.Terminal.feed_child()似乎在Python中是如何生成的。如果将表示参数为Python documentationintNoneC documentation进行比较,则可以看到差异。

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

© www.soinside.com 2019 - 2024. All rights reserved.