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.   v = vte.Terminal ()
  20.   v.connect ("child-exited", lambda term: gtk.main_quit())
  21.   v.fork_command()
  22.   window = gtk.Window()
  23.   window.add(v)
  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_.txt620 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.   v = vte.Terminal ()
  19.   v.connect ("child-exited", lambda term: gtk.main_quit())
  20.   v.fork_command()
  21.   v.feed_child('echo "Hello World"\n')
  22.   window = gtk.Window()
  23.   window.add(v)
  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

to add a pulldown menu, as a trigger to execute shell commands

Great. I did a test and this time all was succesful!

Now i'll try to add a pulldown menu, as a trigger to execute shell commands.
And do you know how to disabe anti-aliasing?

any suggestion?

Thank you for your time,

Steve,

Hi Steve, You need to use the

Hi Steve,

You need to use the set_font_full method. On top of this, you also need to import pango.
The following bit of code will disable anti aliasing:

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

trying to fix

Thanks again chantra, but when i try to run, interpreter shows me:

Traceback (most recent call last):
File "aliasing.py", line 21, in
v.set_font_full(pango.FontDescription(), vte.ANTI_ALIAS_FORCE_DISABLE)
AttributeError: 'module' object has no attribute 'ANTI_ALIAS_FORCE_DISABLE'

And finally, i couldn't get the way to add gui menus to the interpreter...

thanks,

Steve,