# EarthAndMoonGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2009-04-15 File created. # 2009-04-15 Last modification. # This program shows, among other things, how texts can be drawn # with different fonts. # More notes at the end of this file. import gtk import pango class EarthAndMoon : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "BALLS DEPICTING EARTH AND MOON" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 400 ) self.drawing_area = gtk.DrawingArea() self.application_window.add( self.drawing_area ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.application_window.show_all() self.large_font_description = pango.FontDescription( "serif bold 20" ) self.italic_font_description = pango.FontDescription( "serif italic 12" ) self.bold_italic_font_description = \ pango.FontDescription( "serif bold italic 10" ) 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() graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "green" ) this_drawable.draw_arc( graphics_context, True, 30, 30, 212, 212, 0, 360*64 ) # Earth graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "blue" ) this_drawable.draw_arc( graphics_context, True, 400, 100, 58, 58, 0, 360*64 ) # Moon graphics_context.foreground = \ this_drawable.get_colormap().alloc_color( "black" ) text_to_show = self.drawing_area.create_pango_layout( "" ) text_to_show.set_font_description( self.large_font_description ) text_to_show.set_text( "Earth" ) this_drawable.draw_layout( graphics_context, 100, 270, text_to_show ) text_to_show.set_text( "Moon" ) this_drawable.draw_layout( graphics_context, 400, 190, text_to_show ) text_to_show.set_font_description( self.italic_font_description ) text_to_show.set_text( "Equatorial radius: 6380 km, 3960 miles" ) this_drawable.draw_layout( graphics_context, 30, 300, text_to_show ) text_to_show.set_text( "Radius: 1740 km, 1080 miles" ) this_drawable.draw_layout( graphics_context, 350, 220, text_to_show ) text_to_show.set_font_description( self.bold_italic_font_description ) text_to_show.set_text( "Mean distance between Earth and Moon is" ) this_drawable.draw_layout( graphics_context, 200, 330, text_to_show ) text_to_show.set_text( "384 400 kilometers, 238 860 miles" ) this_drawable.draw_layout( graphics_context, 200, 345, text_to_show ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = EarthAndMoon() this_application.run() # NOTES # In PyGTK, fonts can be specified as pango.FontDescription objects. # See http://www.pygtk.org/pygtk2reference/class-pangofontdescription.html # The fonts, in general, are a quite mysterious thing in the world of # computing. Different computers use different fonts. For this reason, # the output of this program may not be the same in every computer. # In this program, texts are drawn as pango.Layout objects. # The upper left corner of the text object is specified as parameters for # the drawing method. # This program has been tested in Ubuntu Linux. In general it seems # that when you define in Linux a font of a certain size, the font that will # be seen on the screen is larger than, for example, the same text size # in Windows.