# HelloGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-10-05 First program version created. # 2006-10-05 Last modification. # This program draws two lines of text onto the screen. # drawing_area_exposed() is the 'paint' method of this application. # It will be called automatically whenever there is a need to # redraw the contents of the window. # A gtk.DrawingArea object is a 'canvas' on which it is possible # to draw various objects, such as lines of text. A DrawingArea is # created in this program, and it is added to a window with the add() # method. # By using a method named connect(), the method that updates the # screen, i.e. the drawing_area_exposed() method, is set to handle # a signal named "expose-event". This signal is received whenever # the drawing area is exposed, i.e., whenever it is necessary to # to update the drawing area. # In this program text_to_show refers to a pango.Layout object # which represents a paragraph of text. With the set_text() method # the actual text inside the object can be set. import gtk class HelloApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.connect( "destroy", gtk.main_quit ) self.application_window.set_size_request( 400, 250 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.application_window.add( self.drawing_area ) self.application_window.show_all() def drawing_area_exposed( self, area, event ) : graphics_context = self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window text_to_show = self.drawing_area.create_pango_layout("") text_to_show.set_text( "Hello. I am a Python/GTK/Linux application." ) this_drawable.draw_layout( graphics_context, 80, 100, text_to_show ) text_to_show.set_text( "The coordinates of this line are (80,150)." ) this_drawable.draw_layout( graphics_context, 80, 150, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__" : this_application = HelloApplication() this_application.run()