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
Submitted by chantra on Sat, 05/24/2008 - 18:39
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-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:
- #!/usr/bin/env python
- try:
- import gtk
- except:
- print >> sys.stderr, "You need to install the python gtk bindings"
- sys.exit(1)
- # import vte
- try:
- import vte
- except:
- error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
- 'You need to install python bindings for libvte')
- error.run()
- sys.exit (1)
- if __name__ == '__main__':
- v = vte.Terminal ()
- v.connect ("child-exited", lambda term: gtk.main_quit())
- v.fork_command()
- window = gtk.Window()
- window.add(v)
- window.connect('delete-event', lambda window, event: gtk.main_quit())
- window.show_all()
- gtk.main()
That's it, download the script attached to this tutorial, make it executable and run it. You will have a functioning terminal!
| Attachment | Size |
|---|---|
| python-vte.py_.txt | 620 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:
would help. You basically give the child process the data the user would have typed.
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:
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,