在 GTK 构件中使用 Python VTE 库嵌入虚拟终端[译]

原文: python embedding virtual terminal gtk widget python vte library

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.

Python-vte 绑定允许你在任意一个GTK 程序中十分容易地嵌入一个简单的虚拟终端。本指南将向您展示如何使用少于30 行代码在一个构件上添加一个终端,其中有4 行代码是设置终端会话所必须的。

A simple Terminal with python-vte

一个简单的python-vte 终端

With python-vte bindings, all that is necessary to have a working terminal is to:

使用python-vte 绑定,生成一个运行的终端必须具备以下条件:

instanciate the vte terminal object

vte 终端对象的一个实例;

tell it what to do when the terminal session is killed via connect child-exited signal

当终端会话结束时,通过使用child-exited 信号连接实例,告诉它怎么做;

and finaly fork a child process which will default to the default user shell

最后fork 一个设置默认用户shell 的子进程。

Here is the bit of code required to add a virtual terminal to a GTK window:

这里有一段代码,要求在GTK 窗体中添加一个虚拟终端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/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!

至此,下载本指南附件中的脚本,赋于可执行权限,然后运行。您将获得一个运行的终端!

参考资料: