Differences between revisions 13 and 14
Revision 13 as of 2005-08-07 08:42:00
Size: 6752
Editor: ehu4ever
Comment:
Revision 14 as of 2005-08-07 08:51:45
Size: 7354
Editor: ehu4ever
Comment:
Deletions are marked like this. Additions are marked like this.
Line 121: Line 121:
18到20行代码是用来检查__name__的值是不是__main__,这样就把程序限定在只有从python直接启动的情况下才会运行,而不是import到了一个正在运行的python解释器。在这种情况下,这上程序就会建造一个新的Base类,而且在 variable base中保存了一个指向它的reference('''''注意:其实这句话我也不是很懂!!!''''')。

程序的最后是调用main()启动GTK+的事件处理循环。

程序运用后,就会出现如下图的一个window:

''''Figure 2.1. Simple PyGTK Window''''
attachment:base.png

含有章节索引的中文 文章模板

::-- ehu4ever [DateTime(2005-08-07T05:40:51Z)] TableOfContents

1. PyGTK 2.0 Tutorial 中文编译

原作者:John Finlay

Version 2.4

April 13, 2005

1.1. 绪论

PyGTK 2.0 是一个系列的Python模块,它提供了一个使用 GTK+ 2.X 的接口。在本篇文档的其它部分中,PyGTK是指PyGTK的2.X版本,GTK 和 GTK+ 是指GTK+的2.X 版本。Python的门户是[http://www.pygtk.org www.pygtk.org]。PyGTK的主要作者是

who is assisted by the developers listed in the AUTHORS file in the PyGTK distribution and the PyGTK community.

Python is an extensible, object-oriented interpreted programming language which is provided with a rich set of modules providing access to a large number of operating system services, internet services (such as HTML, XML, FTP, etc.), graphics (including OpenGL, TK, etc.), string handling functions, mail services (IMAP, SMTP, POP3, etc.), multimedia (audio, JPEG) and cryptographic services. In addition there are many other modules available from third parties providing many other services. Python is licensed under terms similar to the LGPL license and is available for Linux, Unix , Windows and Macintosh operating systems. More information on Python is available at www.python.org . The primary Author of Python is:

GTK (GIMP Toolkit) is a library for creating graphical user interfaces. It is licensed using the LGPL license, so you can develop open software, free software, or even commercial non-free software using GTK without having to spend anything for licenses or royalties.

It's called the GIMP toolkit because it was originally written for developing the GNU Image Manipulation Program (GIMP), but GTK has now been used in a large number of software projects, including the GNU Network Object Model Environment (GNOME) project. GTK is built on top of GDK (GIMP Drawing Kit) which is basically a wrapper around the low-level functions for accessing the underlying windowing functions (Xlib in the case of the X windows system). The primary authors of GTK are:

GTK is currently maintained by:

GTK是一组面象的编程接口(API)。虽然它是用C语言写的,但是它在实现中遵循了“类”和“回调函数”的理念(pointers to functions)。

另外,还存在一个第三方的类,叫GLib,它包含的一些标准的calls,可以作为替代品。而且,GLib还包含了一些处理linked lists的函数,等等。这些可以作为替代的函数增强了GTK的可迁移性,因为GTK中的一些函数实现在另外一些类UNIX系统中是不标准或是没有的,比如g_strerror()。GLib还在一些地方对于libc版本的GTK更优秀,比如g_malloc有更强的可调试性。

在GLib的2.0版本中,使用了作为GTK类继承结构基础的类型系统,这个系统在GTK中使用广泛。有一个线程API,它抽象了各种系统平台的不同的本地线程API,而且还有loading modules的功能。

GTK使用Pango库来实现i18n的文本输出。

这篇文档描述了GTK+的Python接口,以GTK+2.0版本为基础。由Tony Gale和Ian Main编写。目地在于尽可能详细地描述PyGTK,但并不意味着是完美和完整。

这篇文档要求读者对于Python语言、和怎样编写和运行Python程序有一定的了解,如果你对于Python不是很熟,请先搞定[http://www.python.org/doc/current/tut/tut.html Python Tutorial]。这篇文档没有要求读者先前对GTK有了解,如果你是通过PyGTK来学习GTK,请说明你是怎样找到这篇文档的,以及你遇到什么样的困难。

这篇文档没有包括编译Python、GTK和GTK+的说明。

这篇文档以以下类库为基础:

  • GTK+ 2.0 through GTK+ 2.4
  • Python 2.2
  • PyGTK 2.0 through PyGTK 2.4

This document is a "work in progress". Please look for updates on [http://www.pygtk.org www.pygtk.org].

I would very much like to hear of any problems you have learning PyGTK from this document, and would appreciate input as to how it may be improved. Please see the section on Contributing for further information. If you encounter bugs please file a bug at bugzilla.gnome.org against the pygtk project. The information at www.pygtk.org about Bugzilla may help.

The PyGTK 2.0 Reference Manual is available at http://www.pygtk.org/pygtkreference. It describes in detail the PyGTK classes.

The PyGTK website ([http://www.pygtk.org www.pygtk.org]) contains other resources useful for learning about PyGTK including a link to the extensive FAQ and other articles and tutorials and an active maillist and IRC channel (see [http://www.pygtk.org www.pygtk.org] for details).

1.1.1. 浅析PyGTK

这段没什么意思,就不麻烦了

1.2. Getting Started

我们用一段最最esay的代码来开始我们的PyGTK之旅,这个小程序会构造一个200x200 pixel的窗口,它只能用shell才能关掉。

   1 #!/usr/bin/env python
   2 
   3 # example base.py
   4 
   5 import pygtk
   6 pygtk.require('2.0')
   7 import gtk
   8 
   9 class Base:
  10     def __init__(self):
  11         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  12         self.window.show()
  13 
  14     def main(self):
  15         gtk.main()
  16 
  17 print __name__
  18 if __name__ == "__main__":
  19     base = Base()
  20     base.main()

将上面这段代码保存之后,就可以运行了:

python base.py

如果basy.py被你弄成了被执行文件,而且它的存放路径在PATH环境变量中 ,那么它也可以用下面这个命令运行:

base.py

下面是对于整段代码的解释:

第一行是调用python来执行base.py。第五到七行是用来区别装在你pc上的不同版本的PyGTK。这几行代码表明,本程序使用的是PyGTK的2.0版本,它包含了所有主版本号是2的PyGTK。这样这个程序就不会使用你pc上的PyGTK的早期版本(如果有的话)。

18到20行代码是用来检查name的值是不是main,这样就把程序限定在只有从python直接启动的情况下才会运行,而不是import到了一个正在运行的python解释器。在这种情况下,这上程序就会建造一个新的Base类,而且在 variable base中保存了一个指向它的reference(注意:其实这句话我也不是很懂!!!)。

程序的最后是调用main()启动GTK+的事件处理循环。

程序运用后,就会出现如下图的一个window:

'Figure 2.1. Simple PyGTK Window' attachment:base.png

1.2.1. 用PyGTK说“Hello”

1.2.2. Theory of Signals and Callbacks

1.2.3. Events

1.2.4. Stepping Through Hello World

PyGTK2_Tutorial_cn (last edited 2009-12-25 07:16:38 by localhost)