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.

A simple Terminal with python-vteA simple Terminal with python-vte

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:

  1. #!/usr/bin/env python
  2.  
  3. try:
  4.   import gtk
  5. except:
  6.   print >> sys.stderr, "You need to install the python gtk bindings"
  7.   sys.exit(1)
  8.  
  9. # import vte
  10. try:
  11.   import vte
  12. except:
  13.   error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
  14.     'You need to install python bindings for libvte')
  15.   error.run()
  16.   sys.exit (1)
  17.  
  18. if __name__ == '__main__':
  19.   vte = vte.Terminal ()
  20.   vte.connect ("child-exited", lambda term: gtk.main_quit())
  21.   vte.fork_command()
  22.   window = gtk.Window()
  23.   window.add(vte)
  24.   window.connect('delete-event', lambda window, event: gtk.main_quit())
  25.   window.show_all()
  26.   gtk.main()

That's it, download the script attached to this tutorial, make it executable and run it. You will have a functioning terminal!

AttachmentSize
python-vte.py.txt628 bytes

Your code

Thank you so much for posting this! I have been looking for something like this since I started the KernelCheck project last year! Just one question. How do I tell this to start a command as soon as it opens (ex. How xterm -e "echo "Hello, world"" prints 'Hello, world" in the xterm window when it opens)?

master_kernel

vte.feed_child(string) ?

Mabe something like:

  1. #!/usr/bin/env python
  2. try:
  3.   import gtk
  4. except:
  5.   print >> sys.stderr, "You need to install the python gtk bindings"
  6.   sys.exit(1)
  7.  
  8. # import vte
  9. try:
  10.   import vte
  11. except:
  12.   error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
  13.     'You need to install python bindings for libvte')
  14.   error.run()
  15.   sys.exit (1)
  16.  
  17. if __name__ == '__main__':
  18.   vte = vte.Terminal ()
  19.   vte.connect ("child-exited", lambda term: gtk.main_quit())
  20.   vte.fork_command()
  21.   vte.feed_child('echo "Hello World"\n')
  22.   window = gtk.Window()
  23.   window.add(vte)
  24.   window.connect('delete-event', lambda window, event: gtk.main_quit())
  25.   window.show_all()
  26.   gtk.main()

would help. You basically give the child process the data the user would have typed.

python vte execute commandpython vte execute command