Last active
April 2, 2023 13:28
-
-
Save carlos-jenkins/268c562846eec2303e0ffb881c580f4e to your computer and use it in GitHub Desktop.
Example of a Gtk application using a VTE terminal widget written in Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- Generated with glade 3.22.1 --> | |
<interface> | |
<requires lib="gtk+" version="3.20"/> | |
<requires lib="vte-2.91" version="0.52"/> | |
<object class="GtkWindow" id="window"> | |
<property name="can_focus">False</property> | |
<signal name="delete-event" handler="stop" swapped="no"/> | |
<child> | |
<placeholder/> | |
</child> | |
<child> | |
<object class="VteTerminal" id="terminal"> | |
<property name="visible">True</property> | |
<property name="can_focus">True</property> | |
<property name="hscroll_policy">natural</property> | |
<property name="vscroll_policy">natural</property> | |
<property name="encoding">UTF-8</property> | |
<property name="scroll_on_keystroke">True</property> | |
<property name="scroll_on_output">False</property> | |
<signal name="child-exited" handler="_child_exited_cb" swapped="no"/> | |
</object> | |
</child> | |
</object> | |
</interface> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pathlib import Path | |
import gi | |
gi.require_version('Gtk', '3.0') | |
# To make this available install Vte with: | |
# sudo apt install libvte-2.91-dev | |
gi.require_version('Vte', '2.91') | |
from gi.repository import GObject, GLib, Vte, Gtk # noqa | |
GObject.type_register(Vte.Terminal) | |
class KuraTerm(object): | |
def __init__(self): | |
self._builder = Gtk.Builder() | |
self._builder.add_from_file(str( | |
Path(__file__).parent / 'kuraterm.glade' | |
)) | |
# Get objects | |
def go(identifier): | |
widget = self._builder.get_object(identifier) | |
if widget is None: | |
raise ValueError('Widget "{}" not found'.format(identifier)) | |
return widget | |
# Get widgets | |
self._terminal = go('terminal') | |
self._window = go('window') | |
# Connect signals | |
self._builder.connect_signals(self) | |
# Spawn terminal | |
self._child_exited_cb(self._terminal, 0) | |
# Show app | |
self._window.set_default_size(800, 480) | |
self._window.show() | |
def _child_exited_cb(self, term, status, user_data=None): | |
self._terminal.spawn_sync( | |
Vte.PtyFlags.DEFAULT, | |
None, | |
['/bin/bash'], | |
None, | |
GLib.SpawnFlags.DEFAULT, | |
) | |
def start(self): | |
Gtk.main() | |
def stop(self, widget, event, user_data=None): | |
Gtk.main_quit() | |
if __name__ == '__main__': | |
try: | |
app = KuraTerm() | |
app.start() | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment