#!/usr/bin/python # there is this module for obtaining cgi form data #import cgi import sys # define a function to print error messages def error(msg): print """ Error: %s""" % msg print "\n" sys.exit() import psycopg2 #connect to a database try: con = psycopg2.connect(database='timetable', host='cs-linux', user='mickey',password='MoUsE') except: error("Error connecting to server") # make a cursor object for this connection # you can make as many as you like curs = con.cursor() # do a query curs.execute("SELECT pname,given,office,email FROM profs order by pname") # get the resulting table, # can fetchall to get a list of lists # -or- in this program,fetchone,to get a row each time button clicked import pygtk pygtk.require('2.0') import gtk def n2b (str): if str == None: return " " else: return str.rstrip() class Prof: # This is a callback function. The data arguments are ignored # in this example. More on callbacks below. # when button is clicked, get the next row and put pname in label def next_row(self, widget, data=None): row = curs.fetchone() if row != None: self.plabel[1].set_text(n2b(row[0])) self.plabel[0].set_text(n2b(row[1])) self.plabel[2].set_text(n2b(row[2])) self.plabel[3].set_text(n2b(row[3])) 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 gtk.FALSE # Another callback def destroy(self, widget, data=None): curs.close() # close the database connection con.close() print "Connection closed" 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("Next prof") # When the button receives the "clicked" signal, it will call the # function next_row() passing it None as its argument. The next_row() # function is defined above. self.button.connect("clicked", self.next_row, None) # This packs the button into the window (a GTK container). # --- now I add a label to hold pname values self.plabel = [0,1,2,3] self.plabel[0] = gtk.Label("Given name here") self.plabel[1] = gtk.Label("Prof name here") self.plabel[2] = gtk.Label("Office") self.plabel[3] = gtk.Label("e-Mail") self.vbox = gtk.VBox(False, 5) for label in self.plabel: self.vbox.add(label) #self.plabel.show() self.vbox.add(self.button) # The final step is to display this newly created widget. self.button.show() self.window.add(self.vbox) # and the window self.window.show_all() 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__": next_row = Prof() next_row.main()