[Pipet Devel] pygtools tutorial - hello world

J.W. Bizzaro bizzaro at bc.edu
Tue Jun 1 21:10:20 EDT 1999


Below is the classic "Hello World" written in PyG.  I will add comments to each
line.

-------------8<--------------
# Tell UNIX to run this script through Python.
#!/usr/bin/env python

# Tell Python to import all of the classes and methods found in the
# file gtk.py.  This is another Python script and was put in the Python
# module library when you installed PyG Tools.  This script tells Python
# what many of the gtk objects are that are written below.
from gtk import *

# This is a Python "method".  It is just like a procedure or function
# in older programming languages.
def hello(*args):               # The name of the method is "hello".
    print "Hello World"         # This prints to the console/terminal.
    window.destroy()            # This tells GTK to close the window
                                # after printing.

# Another Python method.
def destroy(*args):             # The name of the method is "destroy".
    window.hide()               # This makes the window non-visible.
    mainquit()                  # This exits the whole script.

# This block of code creates the window for us.
window = GtkWindow(WINDOW_TOPLEVEL)   # first we make an instance of
                                      # the GtkWindow() class.
window.connect("destroy", destroy)    # This connects the destroy event
                                      # to the event handler, the destroy
                                      # method we defined above.
window.set_border_width(10)           # Some asthetics.
window.show()                         # Make the window visible.

# This block creates a button, and nearly all widgets follow this pattern.
button = GtkButton("Hello World")     # Make an instance.
button.connect("clicked", hello)      # Connect it to an event handler.
window.add(button)                    # Add it to its parent (window is
                                      # button's parent).
button.show()                         # And show it.

# A loop is created to "catch" widget events, which are of course sent to
# the connect event handlers.
mainloop()
-------------8<--------------

Here is the same script, without all the comments:

-------------8<--------------
#!/usr/bin/env python

>from gtk import *

def hello(*args):
    print "Hello World"
    window.destroy()

def destroy(*args):
    window.hide()
    mainquit()

window = GtkWindow(WINDOW_TOPLEVEL)
window.connect("destroy", destroy)
window.set_border_width(10)
window.show()

button = GtkButton("Hello World")
button.connect("clicked", hello)
window.add(button)
button.show()

mainloop()
-------------8<--------------

Next


-- 
J.W. Bizzaro                  mailto:bizzaro at bc.edu
Boston College Chemistry      http://www.uml.edu/Dept/Chem/Bizzaro/
--




More information about the Pipet-Devel mailing list