PyGTK is a set of Python bindings for the GTK + GUI library. PyGTK is free software and is distributed under the terms of the GNU LGPL . The library was selected as the official development toolkit for the $ 100 Laptop .
| PyGTK | |
|---|---|
| Type of | GUI development |
| Author | James Henstridge Johan dahlin |
| Developers | community |
| Written on | Python C |
| operating system | Linux and others. UNIX-like , Windows |
| Latest version | |
| condition | active |
| License | GNU LGPL |
| Site | pygtk.org |
Starting from version 2.8, wrappers of GLib objects have been moved to a separate library - PyGObject , which should completely supersede PyGTK when using GTK + version 3. Although PyGTK is stable, sufficiently developed and one of the four main GUI libraries for Python, its development was stopped by the authors in 2011 [1] , users were encouraged to switch to PyGObject [2] .
Content
Example: Hello World program.
This example is for Python version 2.x. Other examples can be found in the PyGTK source archive, in the examples folder.
#! / usr / bin / env python
# - * - coding: UTF-8 - * -
import gtk
def button_clicked ( button ):
print 'Hello World!'
def main ():
window = gtk . Window ()
window . set_default_size ( 240 , 180 )
window . set_title ( 'Hello World!' )
window . connect ( 'destroy' , lambda w : gtk . main_quit ())
button = gtk . Button ( 'Press Me' )
button . connect ( 'clicked' , button_clicked )
button . show ()
window . add ( button )
window . present ()
gtk . main ()
if __name__ == '__main__' :
main ()
Garbage collection features
GTK + is implemented in the C language and has its own (not fully integrated with Python) system for taking into account links between GObject objects to delete GTK objects that are no longer used (i.e., GObject garbage collection system will process these objects).
As a result of this, it is possible that links to Python objects (for example, links to callback functions ) stored inside GTK objects (i.e. GObject objects) turn into weak links and can be deleted by the Python garbage collection system. [3]
Errors in the execution of the program, when, for example, the called Callback function begins to operate with uninitialized data (that is, data cleared by the Python garbage collector), may result in the programmer turning links into weak links.
Links will not turn into weak links while the GTK object is still described in the Python program as a PyGTK object (that is, while the program algorithm does not exclude links to the Python object describing the GTK object from the scope). However, GTK objects, adding themselves as children of other GTK objects, do not retain a link to their Python description. However, the programmer can do this (save the Python description of the GTK object) independently, as, for example, this was done in the Hello World program example described above [4] .
Another way to insure against parasitic phenomena when turning links to Python functions into weak links can be a methodology: when GTK objects operate only with links to Python functions that are explicitly stored in the scope of a Python program (while maintaining links PyGTK objects themselves - no longer necessary).
See also
- Pyqt
- TkInter
Notes
- ↑ Summerfield, M. Python in Practice: Create Better Programs Using Concurrency, Libraries, and Patterns. - Pearson Education, 2013 .-- P. 232. - ISBN 9780133373233 .
- ↑ PyGTK: GTK + for Python (official site)
- ↑ This phenomenon is also mentioned in the manual of the ctypes module, designed for transparent interaction of a Python program with binary C code: ctypes - A foreign function library for Python
- ↑ In this example: the variables “window” and “button” (pointing to the Python representation of GTK objects) were saved until the program exited
Literature
- Karvinen, K .; Karvinen, T. Make: Arduino Bots and Gadgets: Six Embedded Projects with Open Source Hardware and Software. - O'Reilly Media, Incorporated, 2011 .-- 278 p. - ISBN 9781449389710 .