Python development masterclass
It may seem like Python is just another scripting language, but know that when you are using Linux, the chances are there is some Python code working backstage helping you. Among other things, this article looks into the versatile nature of Python and its many uses…
Desktop application development
Since the early days of Python, it has had robust support for Tcl/Tk-based support GUI applications called TkInter. Back then it was considered as the de facto standard for GUI app development. Linux oldies will remember that the first interface for kernel configuration was originally written in Tcl/Tk. Starting from the 2.6 series, the kernel configuration interface was available in either Qt or GTK. Since then the world has moved on. Most of the GUI application development is done using GTK, Qt or wxWindows. Python supports most of the current-generation GUI toolkit in the form of PyGTK, PyQt and wxPython.
PyGTK
PyGTK is a library that lets developers write GUI applications using Python and the GTK+ GUI toolkit. As most of you will know, GTK is the backbone of the GNOME desktop and is used for almost all GNOME desktop applications. PyGTK adopts a more liberal licence (LGPL) which makes it the number one choice for commercial software vendors who are often not comfortable with sharing the source code of their application. PyGTK is also fully compliant with the Glade RAD (Rapid Application Development) tool and can be used for rapid application prototyping.
It is important to note that when we use the PyGTK program to build a GUI application, while porting the application to some other platform you will only need the platform-specific binary for PyGTK library and not the main program source code. As Python codes are not interpreted, the same code will run without any modification.
PyGTK leverages the complete technology stack of GTK. GTK technology stack consists of GObject, ATK, GTK, Pango, Cairo and Glade (optional). GObject is a base class providing the common attributes and functions for PyGTK classes. ATK is the accessibility toolkit, which provides tools that help physically challenged people work with computers. GTK is the user interface module. The Pango is a library which is used to work with text and internationalisation. Cairo is a library for creating 2D vector graphics. Finally, Glade is used to build GUI interfaces from XML descriptions.
Hello PyGTK
To get a feel of PyGTK, let’s build a Hello World application using PyGTK. Before running the code, make sure that you have installed the PyGTK package.
Code for:hellopygtk.py
import pygtk import gtk class HelloWorld: # This is a callback function. The data arguments are ignored # in this example. More on callbacks below. def hello(self, widget, data=None): print “Hello World” def delete_event(self, widget, event, data=None): # If you return FALSE in the “delete_event” signal handler, # GTK will emit the “destroy” signal. Returning TRUE means # you don’t want the window to be destroyed. # This is useful for popping up ‘are you sure you want to quit?’ # type dialogs. print “delete event occurred” # Change FALSE to TRUE and the main window will not be destroyed # with a “delete_event”. return False def destroy(self, widget, data=None): print “destroy signal occurred” gtk.main_quit() def __init__(self): # create a new window self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) # When the window is given the “delete_event” signal (this is given # by the window manager, usually by the “close” option, or on the # titlebar), we ask it to call the delete_event () function # as defined above. The data passed to the callback # function is NULL and is ignored in the callback function. self.window.connect(“delete_event”, self.delete_event) # Here we connect the “destroy” event to a signal handler. # This event occurs when we call gtk_widget_destroy() on the window, # or if we return FALSE in the “delete_event” callback. self.window.connect(“destroy”, self.destroy) # Sets the border width of the window. self.window.set_border_width(10) # Creates a new button with the label “Hello World”. self.button = gtk.Button(“Hello World”) # When the button receives the “clicked” signal, it will call the # function hello() passing it None as its argument. The hello() # function is defined above. self.button.connect(“clicked”, self.hello, None) # This will cause the window to be destroyed by calling # gtk_widget_destroy(window) when “clicked”. Again, the destroy # signal could come from here, or the window manager. self.button.connect_object(“clicked”, gtk.Widget.destroy, self.window) # This packs the button into the window (a GTK container). self.window.add(self.button) # The final step is to display this newly created widget. self.button.show() # and the window self.window.show() def main(self): # All PyGTK applications must have a gtk.main(). Control ends here # and waits for an event to occur (like a key press or mouse event). gtk.main() # If the program is run directly or passed as an argument to the python # interpreter then create a HelloWorld instance and show it if __name__ == “__main__”: hello = HelloWorld() hello.main()















Please state which old python version and other programs you are using. It is obvious from your print statements that it is not 3.1.2 which is recommended for ALL new development.
@Frank Python 3.1.2 is not recommended for development as of now. If you had read the article completely you would have known that Python currently maintains two mutually incompatible branch of the 2.X and 3.X. Until 3.X is finalized 2.X will remain in the usage. FYI: 2.X is still used in 95% of the installed applications.