raven_seven
raven_seven

Reputation: 19

How to implement a Linux terminal in a PyGtk app like VScode and PyCharm has?

Problem statement: Embed a terminal Emulator for linux in GUI and feed custom commands to it by GUI components.

using:

Python 3

Gtk+3

Vte.get_minor_version(): 58

Vte.get_major_version(): 0

Vte.get_macro_version(): 2

I have been trying to embed a terminal emulator in a PyGtk app(like the ones in PyCharm and VScode) and feed it system commands by Gtk GUI . I have tried Vte to feed commands to using Terminal.feed_child() method when a button is pressed, but couldn't get it to work.I have tried the following example:

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

can anybody please help me sort this problem out?

are there any alternative approaches to solve the problem statement ?

Upvotes: 1

Views: 860

Answers (4)

corky
corky

Reputation: 185

For the Vte method .feed_child() to work you must first encode the string as UTF-8. As documented here. There are a couple of ways you can do this:

Append .encode('utf-8') to the string

self.command = "echo \"Sending this command to a virtual terminal.\"\n".encode('utf8')

Or add b before the string

self.command = b"echo \"Sending this command to a virtual terminal.\"\n"

Upvotes: 0

Ekure Edem
Ekure Edem

Reputation: 330

Just for people who search. Use

...
def feed_input(self, text):
   text = bytesarray(text, "utf-8")
   self.terminal.feed(text)
...

Upvotes: 0

raven_seven
raven_seven

Reputation: 19

Here is the terminal emulator class which can be packed into a window just like a box.it also has context menu popup functionality

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")                 

Upvotes: -1

ptomato
ptomato

Reputation: 57910

Vte.Terminal.feed_child() seems to have a bug in how it is generated in Python. If you compare the Python documentation which says the argument is int or None to the C documentation then you can see the discrepancy.

I would recommend working around the problem by using feed_child_binary() and a bytes object, and also reporting the bug to PyGObject.

Upvotes: 2

Related Questions