Python: Embedding a Virtual Terminal in a GTK widget with python vte library
Articles may may have files attached at the end of the post
python-vte bindings allow you to embed a simplistic virtual terminal in any of your GTK apps real easy.
This tutorial will show how you can add a terminal to a widget in less than 30 lines, 4 lines only being necessary to set a the terminal session.
With python-vte bindings, all that is necessary to have a working terminal is to:
- instanciate the vte terminal object
- tell it what to do when the terminal session is killed via connect child-exited signal
- and finaly fork a child process which will default to the default user shell
Here is the bit of code required to add a virtual terminal to a GTK window:
- #!/usr/bin/env python
- import gtk
- print >> sys.stderr, "You need to install the python gtk bindings"
- # import vte
- import vte
- error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
- 'You need to install python bindings for libvte')
- sys.exit (1)
- if __name__ == '__main__':
- v = vte.Terminal ()
- v.connect ("child-exited", lambda term: gtk.main_quit())
- window = gtk.Window()
- window.connect('delete-event', lambda window, event: gtk.main_quit())
That's it, download the script attached to this tutorial, make it executable and run it. You will have a functioning terminal!