# DrawingDemoGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-08-08 File created. # 2006-10-02 Last modification. # The development of this program has been sponsored by Nokia Multimedia. import gtk class DrawingDemo : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "DRAWING OPERATIONS DEMONSTRATED" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 500 ) self.drawing_area = gtk.DrawingArea() self.application_window.add( self.drawing_area ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.drawing_area.show() self.application_window.show() 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 window_width, window_height = self.application_window.get_size() text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_text( "Window size is %dx%d " % \ ( window_width, window_height ) ) this_drawable.draw_layout( graphics_context, 20, 20, text_to_show ) this_drawable.draw_line( graphics_context, 0, 100, 500, 100 ) this_drawable.draw_line( graphics_context, 0, 300, 500, 300 ) this_drawable.draw_line( graphics_context, window_width / 2, 0, window_width / 2, window_height ) ; this_drawable.draw_rectangle( graphics_context, True, 20, 50, 100, 40 ) this_drawable.draw_rectangle(graphics_context, True, 20, 200, 100, 80 ) this_drawable.draw_rectangle(graphics_context, False, 10, 190, 120, 100 ) # The following statement copies a specified sub-area to another # location on the drawable area. this_drawable.draw_drawable( graphics_context, this_drawable, 10, 190, 160, 190, 120, 100 ) # Note that angles are expressed in 1/64ths of a degree. # 45 degrees is thus represented by value 45*64 this_drawable.draw_arc( graphics_context, True, 20, 350, 100, 80, 45*64, 270*64 ) this_drawable.draw_arc( graphics_context, True, 170, 350, 100, 80, 315*64, 90*64 ) this_drawable.draw_rectangle( graphics_context, True, 350, 50, 100, 40 ) # By setting the drawing color to background color, we'll be able to # clear a part of the just drawn rectangle. saved_foreground_color = graphics_context.foreground background_color = \ self.drawing_area.get_style().bg[ gtk.STATE_NORMAL ] graphics_context.foreground = background_color this_drawable.draw_rectangle( graphics_context, True, 360, 60, 80, 20 ) # The drawing color will be switched back to the original color. graphics_context.foreground = saved_foreground_color polygon_coordinates = [ ( 400, 150 ), (450, 180 ), ( 450, 220 ), ( 400, 250 ), (350, 220 ), ( 350, 180 ) ] this_drawable.draw_polygon( graphics_context, True, polygon_coordinates ) this_drawable.draw_arc( graphics_context, False, 350, 350, 100, 80, 0, 360*64 ) this_drawable.draw_rectangle( graphics_context, False, 350, 350, 100, 80 ) text_to_show.set_text( "X" ) this_drawable.draw_layout( graphics_context, 350, 350, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = DrawingDemo() this_application.run() # NOTES # The following statement would set the foreground color to # white: # graphics_context.foreground = \ # this_drawable.get_colormap().alloc_color( "white" )