# SumGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # The development of this program has been sponsored by Nokia Multimedia. # 2006-09-11 File created. # 2006-09-14 Last modification. # This program demonstrates the following things: # -- using a Table for placing widgets in a window # -- using class Entry for creating editable text fields # -- using class Label for creating non-editable texts import gtk class Sum : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "CALCULATE THE SUM OF TWO INTEGERS" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 300 ) self.first_text_field = gtk.Entry() self.first_text_field.connect( "changed", self.text_field_text_changed, self.first_text_field ) self.first_text_field.set_alignment( 1.0 ) # right-justified text field self.first_text_field.set_width_chars( 15 ) self.second_text_field = gtk.Entry() self.second_text_field.connect( "changed", self.text_field_text_changed, self.second_text_field ) self.second_text_field.set_alignment( 1.0 ) # right-justified text field first_integer_label = gtk.Label( "First integer:" ) first_integer_label.set_alignment( 0.0, 0.5 ) # left-justified second_integer_label = gtk.Label( "Second integer:" ) second_integer_label.set_alignment( 0.0, 0.5 ) # left-justified calculated_sum_label = gtk.Label( "Calculated sum:" ) calculated_sum_label.set_alignment( 0.0, 0.5 ) # left-justified self.result_label = gtk.Label( "0" ) self.result_label.set_alignment( 1.0, 0.5 ) # The table_for_widgets is larger than is necessary for # placing the widgets. The table cells near the window # border are left empty. Thus the widgets (labels and text # entry fields appear at the center of the window). # The third parameter "True" makes the table homogenous, # which means that all table cells are of equal size. table_for_widgets = gtk.Table( 5, 4, True ) table_for_widgets.attach( first_integer_label, 1, 2, 1, 2 ) table_for_widgets.attach( self.first_text_field, 2, 3, 1, 2 ) table_for_widgets.attach( second_integer_label, 1, 2, 2, 3 ) table_for_widgets.attach( self.second_text_field, 2, 3, 2, 3 ) table_for_widgets.attach( calculated_sum_label, 1, 2, 3, 4 ) table_for_widgets.attach( self.result_label, 2, 3, 3, 4 ) self.application_window.add( table_for_widgets ) self.first_text_field.show() self.second_text_field.show() first_integer_label.show() second_integer_label.show() calculated_sum_label.show() self.result_label.show() table_for_widgets.show() self.application_window.show() def text_field_text_changed( self, widget, text_field=None ) : first_text = self.first_text_field.get_text() second_text = self.second_text_field.get_text() # The following two if constructs ensure that empty text fields # are treated as if they contained a zero. if len( first_text ) == 0 : first_text = "0" if len( second_text ) == 0 : second_text = "0" # An exception will be thrown (raised) if either of the texts # represent of a non-int value. try : first_integer = int( first_text ) second_integer = int( second_text ) self.result_label.set_text( str( first_integer + second_integer ) ) except : self.result_label.set_text( "INPUT ERROR!" ) def run( self ) : gtk.main() if __name__ == "__main__": this_application = Sum() this_application.run()