How to add vte terminal widget in GTK3?

8,551

Solution 1

Here a basic example:

#!/usr/bin/env python

from gi.repository import Gtk, Vte
from gi.repository import GLib
import os

terminal     = Vte.Terminal()
terminal.spawn_sync(
    Vte.PtyFlags.DEFAULT,
    os.environ['HOME'],
    ["/bin/sh"],
    [],
    GLib.SpawnFlags.DO_NOT_REAP_CHILD,
    None,
    None,
    )

win = Gtk.Window()
win.connect('delete-event', Gtk.main_quit)
win.add(terminal)
win.show_all()

Gtk.main()

Solution 2

Building on ADcomp's answer to address wanting to add them dynamically in the future, I think you might want to subclass Vte.terminal.

#!/usr/bin/env python

from gi.repository import Gtk, Vte
from gi.repository import GLib
import os

class MyTerm(Vte.Terminal):
    def __init__(self, *args, **kwds):
        super(MyTerm, self).__init__(*args, **kwds)
        self.spawn_sync(
            Vte.PtyFlags.DEFAULT,
            os.environ['HOME'],
            ["/bin/sh"],
            [],
            GLib.SpawnFlags.DO_NOT_REAP_CHILD,
            None,
            None,
            )

win = Gtk.Window()
win.connect('delete-event', Gtk.main_quit)
bigbox = Gtk.Box()
win.add(bigbox)
bigbox.add(MyTerm())
bigbox.add(MyTerm())
win.show_all()

Gtk.main()

Solution 3

Vte.Terminal.spawn_sync is deprecated. So if you are using Vte version 0.48 or above, use Vte.Terminal.spawn_async instead. Here is an example of a Vte.Terminal.spawn_async:

terminal.spawn_async(
    Vte.PtyFlags.DEFAULT, # Pty Flags
    os.environ['HOME'], # Working DIR
    ["/bin/bash"], # Command/BIN (argv)
    None, # Environmental Variables (envv)
    GLib.SpawnFlags.DEFAULT, # Spawn Flags
    None, None, # Child Setup
    -1, # Timeout (-1 for indefinitely)
    None, # Cancellable
    None, # Callback
    None # User Data
)

Solution 4

Since VTE 0.38, vte_terminal_fork_command_full () has been renamed to vte_terminal_spawn_sync (). So if you are using newer versions, you have to change @ADcomp's answer into the following:

terminal.spawn_sync(
    Vte.PtyFlags.DEFAULT,
    os.environ['HOME'],
    ["/bin/sh"],
    [],
    GLib.SpawnFlags.DO_NOT_REAP_CHILD,
    None,
    None,
    )
Share:
8,551

Related videos on Youtube

int_ua
Author by

int_ua

I dream of a day when settings are written to disk only when changed and software does not freak out when $HOME is read-only.

Updated on September 18, 2022

Comments

  • int_ua
    int_ua over 1 year

    I'm trying to add vte widget in my application and the examples I've found use .fork_command() to execute a command in that widget. But according to

    http://developer.gnome.org/vte/0.26/VteTerminal.html#vte-terminal-fork-command

    it was deprecated and it's recommended to use fork_command_full(). Which needs eight mandatory arguments. Haven't they heard the "defaults" word? I've been able to construct lines that work somehow:

    pty_flags = vte.PtyFlags(0)
    terminal.fork_command_full(pty_flags, "/home/int", ("/bin/bash", ), "", 0, None, None)
    

    Yes, I know about the enums, I just hope that I'm doing this completely wrong and there is a much easier way. Do you know any?

    P.S. I'm using quickly with the default ubuntu-application template.

    P.P.S. The import line is from gi.repository import Vte as vte

    • Admin
      Admin almost 12 years
      you have that long line a bunch of times or just once?
    • Admin
      Admin almost 12 years
      just once right now but I want to add them dynamically in the future.
  • int_ua
    int_ua almost 12 years
    Thanks :) __init__(self, command) would be even nicer, I'll try this later.
  • RobotHumans
    RobotHumans almost 12 years
    @int_ua - more what you wanted? i thought you were just looking to embed a term from the context of the question
  • ThorSummoner
    ThorSummoner over 9 years
    How can I catch/handle/recover from a user submitting 'exit' in the terminal? On exit would I need to spawn a new vte terminal? or perhaps just spawn a new shell inside the terminal? Additionally, can you link to documentation on how to manipulate the vte terminal, eg opening bash on click of a gtk button press?
  • PizzaLovingNerd
    PizzaLovingNerd over 3 years
    This is my first AskUbuntu post :D
  • shevy
    shevy about 3 years
    Thanks - I am using ruby-gtk bindings but indeed that change I did not notice, so this is useful.
  • shevy
    shevy about 3 years
    It is even useful for non-python; for example, ruby code is almost the same as the python code, just with some syntax difference e. g. GLib.SpawnFlags would be Glib::SpawnFlags - so this information is useful even for non-python folks. (I do use python too, though.)